17

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.

I have done some research on SO and get the basic looping but I can't seem to get past the first level.

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?

str
  • 42,689
  • 17
  • 109
  • 127
timmackay
  • 1,026
  • 4
  • 13
  • 27
  • `customers.cluster[0].segment[0].node[0].xpos`, the `0` values may be swapped with other numbers. – zzzzBov Mar 04 '11 at 03:42
  • possible duplicate of [Traverse all the Nodes of a JSON Object Tree with JavaScript](http://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with-javascript) – cimmanon Aug 18 '15 at 13:05

1 Answers1

20

You have an object (customers) with an array stored at cluster, which you can iterate through with

var i, cluster;
for (i = 0; i < customers.cluster.length; i++)
{
  cluster = customers.cluster[i];
}

cluster has an array stored at segment which you can iterate through with:

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

segment has an array stored at node which you can iterate through with:

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
    }
  }
}
Joseph Casey
  • 1,283
  • 1
  • 14
  • 34
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 4
    @zzzBov: lolz thank you for spelling it out for me :) Believe it or not this isn't homework, I am just reeeeallly slow picking up this sort of stuff. I need a bit of hand holding in the beginning and I appreciate when people (like you!) take the time to go into detail. Cheers. – timmackay Mar 04 '11 at 04:00
  • 8
    Good explination, but the homework comment is patronising. The take away here for me, isn't the effort given in the answer but the dissmissal at the end. – orionrush Sep 30 '13 at 14:14