我有一個3級嵌套的JavaScript對象。我很難從第三層嵌套中獲取值。如何循環瀏覽JavaScript對象的深層嵌套屬性?
我已經做了一些關於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"
}]
}]
}]
};
我如何遍歷和檢索XPOS和ypos從節點內?
'customers.cluster [0] .SEGMENT [0]。節點[0] .xpos','0'值可以與其他數字交換。 – zzzzBov 2011-03-04 03:42:35
[使用JavaScript遍歷JSON對象樹的所有節點]的可能重複(http://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with- javascript) – cimmanon 2015-08-18 13:05:30