如何循环遍历 JavaScript 对象的深层嵌套属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5189387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I loop through deeply nested properties of a JavaScript object?
提问by timmackay
I have a JavaScript object with 3 levels of nesting. I am having a hard time getting the values from the 3rd level of nesting.
我有一个具有 3 级嵌套的 JavaScript 对象。我很难从第 3 级嵌套中获取值。
I have done some research on SO and get the basic looping but I can't seem to get past the first level.
我已经对 SO 进行了一些研究并获得了基本的循环,但我似乎无法通过第一级。
Here is my code
这是我的代码
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"
}]
}]
}]
};
How do I loop through and retrieve xpos and ypos from within node?
如何从节点内循环并检索 xpos 和 ypos?
回答by zzzzBov
You have an object (customers) with an array stored at cluster, which you can iterate through with
您有一个对象 ( customers) cluster,其中存储了一个数组,您可以使用它进行迭代
var i, cluster;
for (i = 0; i < customers.cluster.length; i++)
{
cluster = customers.cluster[i];
}
clusterhas an array stored at segmentwhich you can iterate through with:
cluster存储了一个数组segment,您可以在其中迭代:
var j, segment;
for (j = 0; j < cluster.segment.length; j++)
{
segment = cluster.segment[j];
}
segmenthas an array stored at nodewhich you can iterate through with:
segment存储了一个数组node,您可以在其中迭代:
var k, node;
for (k = 0; k < segment.node.length; k++)
{
node = segment.node[k];
}
You can combine all of these to iterate through every node of every segment of every cluster on customers just by combining these loops:
您可以将所有这些组合起来,通过组合这些循环来遍历客户上每个集群的每个段的每个节点:
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
}
}
}

