david
david

Reputation: 6805

How do to a nested for loop in this example with javascript

var data = [
    {
    "something": "something",
    "stages": [{
                "node": {"name": "test0"},
                "status": {"name": "test"},
                "time": {"name": "test"}
               },{
                "node": {"name": "test1"},
                "status": {"name": "test"},
                "time": {"name": "test"}
               }]
     }
];

nodeList = []

data.forEach(obj =>
  obj.stages.forEach(stage => if (nodeList.indexOf(stage.node.name) > -1) {
   nodeList.push({stage.node.name})
  );

I am trying to add the name to a list if that name does not already exist in the list. The above is not working.

Upvotes: 1

Views: 84

Answers (5)

WilliamNHarvey
WilliamNHarvey

Reputation: 2485

For...in loops in javascript return strings, not objects. You can either continue treating it as a string, or parse it into an object to loop over.

Also totally confused about you redefining i, it's not going to work unless you make a different variable in your first loop

for (field in this.job[0].stages[i]) {
  console.log(field);
    for (node in this.job[0].stages[i][field]) {
      console.log(node);
      console.log(this.job[0].stages[i][field][node].name);
    }
}

EDIT:

here, from what you've put in the OP I've fixed your syntax errors

var data = [
  {
  "something": "something",
  "stages": [{
              "node": {"name": "test0"},
              "status": {"name": "test"},
              "time": {"name": "test"}
             },{
              "node": {"name": "test1"},
              "status": {"name": "test"},
              "time": {"name": "test"}
             }]
   }
];

nodeList = [];

data.forEach(obj =>
  obj.stages.forEach(stage => {
    if (nodeList.indexOf(stage.node.name) === -1) {
      return nodeList.push(stage.node.name)
    }
  })
);

Upvotes: 2

Jaya
Jaya

Reputation: 3921

Assuming you would like to print all the values for the name properties for each of the keys present, (ie) node, status and time, you could do the following,

var sampleJobs = [{
  "something": "something",
  "stages": [{
      "node": {
        "name": "node0-test"
      },
      "status": {
        "name": "status0-test"
      },
      "time": {
        "name": "time0-test"
      }
    },
    {
      "node": {
        "name": "node1-test"
      },
      "status": {
        "name": "status1-test"
      },
      "time": {
        "name": "time1-test"
      }
    }
  ]
}]

// outer for loop is for the array of the job object itself
for (i = 0; i < sampleJobs.length; i++) {
 
  // inner for loop is for the stages object within the outer job object
  for (j = 0; j < sampleJobs[i].stages.length; j++) {
    // this will print each object inside the stages array 
    // console.log(sampleJobs[i].stages[j]);

    // now am making an assumption that you want to print the value for "name" property foreach of the keys, here is how you do it
    // this will print the name value for all the keys present in the object
    
  Object.keys(sampleJobs[i].stages[j]).forEach(e => {
       console.log(sampleJobs[i].stages[j][e]["name"])
    })
 
  }

}

Note: Feel free to comment or uncomment the console statements which i have placed here as they are all for undertanding purpose only.

Upvotes: 0

RobG
RobG

Reputation: 147503

Given the object literal you've added to the OP (and fixing the syntax errors), the data object is an array of objects which have a stages property.

stages is also an array of objects that have a node property, which is an object with a name property.

You shouldn't use for..in for arrays as the order is not guaranteed. Assuming that you need it to be, you should use forEach, which also makes the code much more concise:

var data = [
    {
    "something": "something",
    "stages": [{
                "node": {"name": "test0"},
                "status": {"name": "test"},
                "time": {"name": "test"}
               },{
                "node": {"name": "test1"},
                "status": {"name": "test"},
                "time": {"name": "test"}
               }]
     }
];

data.forEach(obj =>
  obj.stages.forEach(stage => console.log(stage.node.name))
);

Upvotes: 0

musicfuel
musicfuel

Reputation: 1680

As the others were mentioning, the i is just a string in this case because it is the name of a key which is just a string. To get the value of i in your structure you need to fully reference the object using it again:

nodeName() {
  if (this.job[0].stages[0]) {
    for (i in this.job[0].stages[i]) {
      console.log(i);
      for (node in this.job[0].stages[i].node) {
        console.log(node.name)
      }
    }
  }
}

Upvotes: 0

Indecisive
Indecisive

Reputation: 76

Why not try:

function nodeName () {
  if (this.job[ 0 ].stages[ 0 ]) {
    for (let i = 0; i < this.job[ 0 ].stages.length; i++) {
      let name = this.job[ 0 ].stages[ i ].node.name
      if (name)
        console.log(name) // "test"
    }
  }
}

Upvotes: 0

Related Questions