gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:21:38 25 4
gpt4 key购买 nike

这是 JSON,具体问题有注释 //does not get parsed current

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"
}
}
}
}
}
}];

我认为递归运行得很好,但随后出现了上面的边缘情况,即在存在“值”属性的对象中给出了附加属性。我有几个选择,我可以以不同的方式输出 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

最佳答案

你可以使用类似的东西

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

关于使用 hasOwnProperty 的 JavaScript/JSON 递归逻辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23200667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com