[英]Javascript - Extract values from nested object
我对编程相当陌生(2-3 个月),我正在努力寻找解决此问题的方法:
我的目标:我创建了一个带有幻想位置的嵌套对象。 我想从我生成的每个“位置”访问 pop 值,并求和以了解总人口。
我被卡住的地方:我可以从主要对象及其直接子对象中找到并添加流行音乐,但对于孩子的孩子来说似乎不起作用。
这是一个示例嵌套树,具有父位置 (Id:1)、一个子节点 (Id: 2) 和 Id 2 的两个孙子节点 (id: 3 和 4)。
[{"value":{
"name":"Bridgejade",
"type":"metropolis",
"pop":12058,
"rulName":[1,"Guzasa Rocwel"],
"rulTitle":["king","queen"],
"Id":"1",
"Parent":""
},
"children":[{
"value":{
"name":"Bluriver",
"type":"town",
"pop":2830,
"rulName":[1,"Esasadryd Bravrose"],
"rulTitle":["count","countess"],
"Id":"2",
"Parent":"1"
},
"children":[{
"value":{
"name":"Lightlanding",
"type":"village",
"pop":382,
"rulName":[0,"Barta Kal Eriin"],
"rulTitle":["baron","baroness"],
"Id":"3",
"Parent":"2"
}
},{
"value":{
"name":"Syldov",
"type":"village",
"pop":297,
"rulName":[0,"Sinne Whitelel"],
"rulTitle":["baron","baroness"],
"Id":"4",
"Parent":"2"
}
}]
}]
}]
我创建了一个函数来计算总人口。 这是我到目前为止所拥有的:
function findTotPop(x) {
var totalPop = 0;
var childrenPop = 0;
totalPop += x[0].value.pop;
if (x[0].children.length >= 1) {
}
for (var enfant = 0; enfant <= (x[0].children.length - 1); enfant++) {
if (x[0].children[enfant].children.length >= 1) {
alert(enfant + " à " + x[0].children[enfant].children.length + " pEnfant")
}
for (var pEnfant = 0; pEnfant < x[0].children[enfant].children.length; pEnfant++) {
console.log(x[0].children[enfant].children[pEnfant].value.pop)
}
var childrenPop = childrenPop + x[0].children[enfant].value.pop
};
console.log(x[0].children[0].children.length)
totalPop += childrenPop
return "La population totale est de " + totalPop;
}
我不断收到此错误:未捕获的类型错误:无法在 findTotPop (genFunctions.js:164) 处读取未定义的属性“长度”
任何帮助,将不胜感激!
请添加一个 if 条件检查来检查是否有任何子项要实际检查,如下所示,for 循环需要在 if 条件内,以便在没有子项时不会出错。 您还可以链接空检查,如下所示。
if (x[0] && x[0].children && x[0].children.length >= 1) {
通过这样做,如果该对象中没有子对象,则上面的代码不会导致错误,而是返回 false,如果我们链接和条件如上所示,如果所有结果都不为假,则只会返回最后一个值。
const data = [{ "value": { "name": "Bridgejade", "type": "metropolis", "pop": 12058, "rulName": [1, "Guzasa Rocwel"], "rulTitle": ["king", "queen"], "Id": "1", "Parent": "" }, "children": [{ "value": { "name": "Bluriver", "type": "town", "pop": 2830, "rulName": [1, "Esasadryd Bravrose"], "rulTitle": ["count", "countess"], "Id": "2", "Parent": "1" }, "children": [{ "value": { "name": "Lightlanding", "type": "village", "pop": 382, "rulName": [0, "Barta Kal Eriin"], "rulTitle": ["baron", "baroness"], "Id": "3", "Parent": "2" } }, { "value": { "name": "Syldov", "type": "village", "pop": 297, "rulName": [0, "Sinne Whitelel"], "rulTitle": ["baron", "baroness"], "Id": "4", "Parent": "2" } }] }] }]; console.log(findTotPop(data)); function findTotPop(x) { var totalPop = 0; var childrenPop = 0; totalPop += x[0].value.pop; if (x[0] && x[0].children && x[0].children.length >= 1) { for (var enfant = 0; enfant <= (x[0].children.length - 1); enfant++) { if (x[0].children && x[0].children[enfant] && x[0].children[enfant].children && x[0].children[enfant].children.length >= 1) { for (var pEnfant = 0; pEnfant < x[0].children[enfant].children.length; pEnfant++) { childrenPop = childrenPop + x[0].children[enfant].value.pop } } }; } totalPop += childrenPop return "La population totale est de " + totalPop; }
谢谢纳伦! 根据您的建议和一些调整,我设法让它发挥作用! 这是我最终为函数 findTotPop 想出的代码:
function findTotPop(a) {
var totalPop = 0;
var childrenPop = 0;
totalPop += a[0].value.pop;
if (a[0].children && a[0].children.length >= 1) {
for (var i = 0; i <= (a[0].children.length - 1); i++) {
childrenPop = childrenPop + a[0].children[i].value.pop
if (a[0].children && a[0].children[i] && a[0].children[i].children && a[0].children[i].children.length >= 1) {
for (var y = 0; y < a[0].children[i].children.length; y++) {
childrenPop = childrenPop + a[0].children[i].children[y].value.pop
}
}
};
}
totalPop += childrenPop
return "The total population is " + totalPop;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.