使用hasOwnProperty的JavaScript / JSON递归逻辑问题

时间:2014-04-21 15:44:04

标签: javascript jquery json

以下是JSON,具体问题有评论// doesn't get parsed currently

var oarsObject = [{
    "coordinateReferenceSystem": "26782,15851 <-- not in a value",
        "positionReferenceType": "geogWgs84",
        "geogWgs84": {
        "latitude": {
            "value": 0.50018514607964015
        },
            "longitude": {
            "value": -1.587237735514361
        },
            "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": {
        "value": "26782,15851 <-- was in a value"
    },
        "positionReferenceType": {
        "value": "geogWgs84"
    },
        "geogWgs84": {
        "latitude": {
            "value": 0.5001841257180204
        },
            "longitude": {
            "value": -1.5869069927615211
        },
            "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
        "positionReferenceType": "geogWgs84",
        "geogWgs84": {
        "latitude": {
            "value": 0.49997645018241949
        },
            "longitude": {
            "value": -1.5869078356348387
        },
            "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
        "positionReferenceType": "geogWgs84",
        "geogWgs84": {
        "latitude": {
            "value": 0.4999774704207946
        },
            "longitude": {
            "value": -1.5872385394554729
        },
            "height": {
            "value": 0.0
        }
    },
        "nest": {
        "level_1": {
            "child_1": {
                "value": "child_1 <-- level 1",
                "gchild_1": {"value": "gchild_1 <-- level 2"} // doesn't get parsed currently
            },
                "child_2": {
                "gchild_1": {
                    "value": "gchild_1 <-- level 2"
                }
            },
                "child_3": {
                "gchild_2": {
                    "ggchild_1": {
                        "value": "ggchild_1 <-- level 3"
                    }
                }
            }
        }
    }
}];

我认为递归工作得很好,但是上面的边缘情况出现了在存在“value”属性的对象中给出了附加属性的地方。我有几个选择,我可以用不同的方式吐出JSON或者我可以尝试解释这些边缘情况,这对我来说似乎更具可扩展性。这是我现在迭代和递归的方式 -

function propertyTest(currentObject) {
    for (var property in currentObject) {
        if (currentObject[property].hasOwnProperty('value')) {
            // if the "value" shoe fits
            $('#method1').append(property + ', ' + currentObject[property].value + '<br />');
        } else if (typeof currentObject[property] === "object") {
            // recurses when needed
            propertyTest(currentObject[property]);
        } else {
            // spits out "valueless" info
            $('#method1').append(property + ' ' + currentObject[property] + '<br />');
        }
    }
}

附加到div只是一种方便输出测试的方法。代码可能不像现在那样整洁,但我会在测试后将其清理干净。

问题 - 如何正确递归/迭代以便我可以检索这些边缘案例值?任何建议或帮助将非常感谢!

FIDDLE

1 个答案:

答案 0 :(得分:1)

您可以使用类似

的内容
function propertyTest(currentObject) {
    for (var property in currentObject) {
        if (currentObject[property].hasOwnProperty('value')) {
            $('#method1').append(property + ', ' + currentObject[property].value + '<br />');
        } 
        if (typeof currentObject[property] === "object") {
            propertyTest(currentObject[property]);
        } else if(property != 'value') {
            $('#method1').append(property + ' ' + currentObject[property] + '<br />');
        }
    }
}

Demo

我将简化为:

function propertyTest(currentObject, key) {
    for (var property in currentObject) {
        if (typeof currentObject[property] === "object") {
            propertyTest(currentObject[property], property);
        } else {
            $('#method1').append(
                (property=='value' && key ? key+',' : property)
                + ' ' + currentObject[property] + '<br />'
            );
        }
    }
}

Demo