Tengo un objeto de JavaScript con 3 niveles de anidación. Estoy teniendo dificultades para obtener los valores del 3er nivel de anidación.¿Cómo recorro las propiedades profundamente anidadas de un objeto JavaScript?
He hecho algunas investigaciones sobre SO y obtengo el bucle básico, pero parece que no puedo pasar del primer nivel.
Aquí está mi código
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"
}]
}]
}]
};
¿Cómo puedo recorrer y recuperar xpos y ypos desde dentro del nodo?
'customers.cluster [0] .SEGMENT [0]. nodo [0] .xpos', los valores '0' pueden intercambiarse con otros números. – zzzzBov
posible duplicado de [Recorrido de todos los nodos de un árbol de objetos JSON con JavaScript] (http://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with- javascript) – cimmanon