开发者问题收集

Javascript - 从嵌套对象中提取值

2020-03-23
83

我对编程还很陌生(2-3 个月),我正在努力寻找解决这个问题的方法:

我的目标:我创建了一个带有幻想地点的嵌套对象。我想从我生成的每个“地点”访问 pop 值,并求和以了解总人口。

我遇到的问题:我可以找到并添加主对象及其直接子对象的 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;
}

我不断收到此错误:未捕获 TypeError:无法读取未定义的属性“length” at findTotPop (genFunctions.js:164)

如能提供任何帮助,我们将不胜感激!

2个回答

请添加 if 条件检查,以检查是否有任何子项需要实际检查,如下所示,for 循环需要位于 if 条件内,以便在没有子项时不会出错。您还可以链接 null 检查,如下所示。

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;
}
Naren Murali
2020-03-23

谢谢 Naren!根据您的建议和一些调整,我终于让它工作了!这是我最终为函数 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;
}

Dimon Birl
2020-03-23