在此示例中如何使用javascript进行嵌套的for循环

时间:2019-06-03 23:06:36

标签: javascript vue.js

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})
  );

如果列表中尚不存在该名称,则尝试将其添加到列表中。上面的方法不起作用。

5 个答案:

答案 0 :(得分:2)

For ... in循环在javascript中返回字符串,而不是对象。您可以继续将其视为字符串,也可以将其解析为对象以进行循环。

对于您重新定义i也完全感到困惑,除非您在第一个循环中输入其他变量,否则它将不起作用

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);
    }
}

编辑:

在这里,根据您在OP中的内容,我已修复了语法错误

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)
    }
  })
);

答案 1 :(得分:0)

为什么不尝试:

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"
    }
  }
}

答案 2 :(得分:0)

正如其他人所提到的,在这种情况下,i只是一个字符串,因为它是密钥的名称,而密钥只是一个字符串。要获得结构中i的值,您需要再次使用它完全引用该对象:

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)
      }
    }
  }
}

答案 3 :(得分:0)

鉴于您已添加到OP中的对象文字(并修复了语法错误),数据对象是具有 stages 属性的对象数组。

stages 也是具有 node 属性的对象的数组,该属性是具有 name 属性的对象。

您不应该对数组使用 for..in ,因为不能保证顺序。假设您需要使用它,则应使用 forEach ,这也使代码更加简洁:

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))
);

答案 4 :(得分:0)

假设您想为存在的每个键(即namenodestatus打印time属性的所有值,则可以执行以下操作,

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"])
    })
 
  }

}

注意: 请随意注释或取消注释我在此处放置的控制台语句,因为它们仅供参考。