首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何遍历JavaScript对象的深度嵌套属性?

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

Stack Overflow用户
提问于 2011-03-04 11:40:10
回答 1查看 19.4K关注 0票数 17

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

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

以下是我的代码

代码语言:javascript
复制
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"
         }]
      }]
   }]
};

如何在节点中遍历和检索xpos和ypos?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-04 11:49:40

您有一个对象(customers),其中的一个数组存储在cluster中,您可以使用以下命令遍历该对象

代码语言:javascript
复制
var i, cluster;
for (i = 0; i < customers.cluster.length; i++)
{
  cluster = customers.cluster[i];
}

clustersegment中存储了一个数组,您可以使用以下命令遍历该数组:

代码语言:javascript
复制
var j, segment;
for (j = 0; j < cluster.segment.length; j++)
{
  segment = cluster.segment[j];
}

segmentnode中存储了一个数组,您可以使用以下命令遍历该数组:

代码语言:javascript
复制
var k, node;
for (k = 0; k < segment.node.length; k++)
{
  node = segment.node[k];
}

您可以组合所有这些循环,只需组合以下循环即可遍历客户上每个集群的每个细分的每个节点:

代码语言:javascript
复制
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
    }
  }
}
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5189387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档