Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何循环遍历JavaScript对象的深度嵌套属性?_Javascript - Fatal编程技术网

如何循环遍历JavaScript对象的深度嵌套属性?

如何循环遍历JavaScript对象的深度嵌套属性?,javascript,Javascript,我有一个JavaScript对象,有3个嵌套级别。我很难从第三层嵌套中获得值 我对SO做了一些研究,得到了基本的循环,但我似乎无法通过第一级 这是我的密码 var customers = { "cluster": [{ "id": "cluster1.1", "color": "blue", "flights": "784", "profit": "524125", "clv": "2364", "segment":

我有一个JavaScript对象,有3个嵌套级别。我很难从第三层嵌套中获得值

我对SO做了一些研究,得到了基本的循环,但我似乎无法通过第一级

这是我的密码

var customers = {
   "cluster": [{
      "id": "cluster1.1",
      "color": "blue",
      "flights": "784",
      "profit": "524125",
      "clv": "2364",
      "segment": [{
         "id": "segment1.1",
         "color": "green",
         "flights": "82",
         "profit": "22150",
         "clv": "1564",
         "node": [{
            "id": "node1.1",
            "color": "orange",
            "xpos": "1",
            "ypos": "1"
         }, {
            "id": "node1.2",
            "color": "orange",
            "xpos": "1",
            "ypos": "2"
         }, {
            "id": "node1.3",
            "color": "orange",
            "xpos": "1",
            "ypos": "3"
         }, {
            "id": "node1.4",
            "color": "orange",
            "xpos": "1",
            "ypos": "4"
         }]
      }, {
         "id": "segment1.2",
         "color": "red",
         "flights": "2",
         "profit": "2150",
         "clv": "1564",
         "node": [{
            "id": "node2.1",
            "color": "tan",
            "xpos": "2",
            "ypos": "1"
         }, {
            "id": "node2.2",
            "color": "tan",
            "xpos": "2",
            "ypos": "2"
         }, {
            "id": "node2.3",
            "color": "tan",
            "xpos": "2",
            "ypos": "3"
         }, {
            "id": "node2.4",
            "color": "tan",
            "xpos": "2",
            "ypos": "4"
         }]
      }]
   }, {
      "id": "cluster1.2",
      "flights": "4",
      "profit": "5245",
      "clv": "2364",
      "segment": [{
         "id": "segment1.2",
         "flights": "2",
         "profit": "2150",
         "clv": "1564",
         "node": [{
            "id": "node3.1",
            "xpos": "3",
            "ypos": "1"
         }, {
            "id": "node3.2",
            "xpos": "3",
            "ypos": "2"
         }, {
            "id": "node3.3",
            "xpos": "3",
            "ypos": "3"
         }, {
            "id": "node3.4",
            "xpos": "3",
            "ypos": "4"
         }]
      }]
   }, {
      "id": "cluster1.3",
      "flights": "10",
      "profit": "456978",
      "clv": "548",
      "segment": [{
         "id": "segment1.3",
         "flights": "2",
         "profit": "2150",
         "clv": "1564",
         "node": [{
            "id": "node4.1",
            "xpos": "4",
            "ypos": "1"
         }, {
            "id": "node4.2",
            "xpos": "4",
            "ypos": "2"
         }, {
            "id": "node4.3",
            "xpos": "4",
            "ypos": "3"
         }, {
            "id": "node4.4",
            "xpos": "4",
            "ypos": "4"
         }]
      }]
   }]
};
如何在节点内循环并检索XPO和YPO?

您有一个对象(
客户
),其中一个数组存储在
集群
,您可以使用它进行迭代

var i, cluster;
for (i = 0; i < customers.cluster.length; i++)
{
  cluster = customers.cluster[i];
}
在节点上存储了一个数组,您可以使用该数组进行迭代:

var j, segment;
for (j = 0; j < cluster.segment.length; j++)
{
  segment = cluster.segment[j];
}
var k, node;
for (k = 0; k < segment.node.length; k++)
{
  node = segment.node[k];
}
vark,节点;
对于(k=0;k
您可以将所有这些结合起来,只需结合以下循环,即可迭代客户上每个集群的每个分段的每个节点:

var i, cluster, j, segment, k, node;
for (i = 0; i < customers.cluster.length; i++)
{
  cluster = customers.cluster[i];

  for (j = 0; j < cluster.segment.length; j++)
  {
    segment = cluster.segment[j];

    for (k = 0; k < segment.node.length; k++)
    {
      node = segment.node[k];
      //access node.xpos, node.ypos here
    }
  }
}
var i,簇,j,段,k,节点;
对于(i=0;i
customers.cluster[0]。segment[0]。node[0]。xpos
0
值可以与其他数字交换。可能重复@zzzBov:lolz谢谢你帮我拼出来:)信不信由你,这不是家庭作业,我只是学这类东西的速度太慢了。在开始的时候我需要一点牵手,我很感激人们(像你一样!)花时间去了解细节。干杯。很好的解释,但是家庭作业的评论是高人一等的。对我来说,这里的收获不是答案中给出的努力,而是最后的失望。