for / in循环中的for / in循环

时间:2018-04-11 16:11:18

标签: javascript for-in-loop

我很想知道你是否可以使用标准for循环在for / in循环中放置for / in循环。在下面的例子中,我试图列出每个游戏对象的所有属性,但我没有得到预期的结果。我尝试过使用点符号和方括号,但似乎都不起作用。这是不可能的还是我在语法中忽略了一些简单的东西?



var games = [{
    "name": "PCH Mystery Game",
    "maxScore": 50000,
    "levels": 4,
    "players": ["akshat", "joe", "dandan", "andrew"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Token Dash",
    "maxScore": 11500,
    "levels": 2,
    "players": ["andrew", "dandan", "matt", "mitchell", "hadi"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Balloon Drop",
    "maxScore": 500,
    "levels": 1,
    "players": [
      "akshat", "joe", "dandan", "dan", "mark", "nagesh", "jessie", "lou"
    ],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Envelope Toss",
    "maxScore": 7000,
    "levels": 84,
    "players": ["akshat", "jessie", "joe", "andrew", "dandan"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Prize Patrol Race",
    "maxScore": 100000,
    "levels": 5,
    "players": [
      "akshat", "joe", "andrew", "dandan", "lou", "roberto", "jessie", "haim", "matt", "mitchell", "ian"
    ],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  }
];

for (game in games) {
  document.write("game: " + games[game].name + "<br>");
  for (prop in game) {
    document.write("property name: " + prop + "<br>");
    document.write("property value: " + game[prop] + "<br>");
    document.write("property value: " + game.prop + "<br>");
  }
  document.write("<br>");
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:5)

从根本上说,是的,您可以使用嵌套的for-in循环。这不是代码中的问题。

for-in循环中的变量是属性名称(或键),而不是实际值。另外,for-in并不是循环数组的正确方法(尽管如果你尝试的话可以使它工作)。 See my answer here获取循环数组的方法列表。

在这种情况下,你可能会使用forEach或(在现代环境中)for-of或只是一个简单的for循环来通过数组的外部循环,然后一个for-in循环(在对象属性上)

games.forEach(function(game) {
    for (var prop in game) {
        console.log("property name: " + prop);
        console.log("property value: " + game[prop]);
    }
});

直播示例:

&#13;
&#13;
var games = [{
    "name": "PCH Mystery Game",
    "maxScore": 50000,
    "levels": 4,
    "players": ["akshat", "joe", "dandan", "andrew"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Token Dash",
    "maxScore": 11500,
    "levels": 2,
    "players": ["andrew", "dandan", "matt", "mitchell", "hadi"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Balloon Drop",
    "maxScore": 500,
    "levels": 1,
    "players": [
      "akshat", "joe", "dandan", "dan", "mark", "nagesh", "jessie", "lou"
    ],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Envelope Toss",
    "maxScore": 7000,
    "levels": 84,
    "players": ["akshat", "jessie", "joe", "andrew", "dandan"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Prize Patrol Race",
    "maxScore": 100000,
    "levels": 5,
    "players": [
      "akshat", "joe", "andrew", "dandan", "lou", "roberto", "jessie", "haim", "matt", "mitchell", "ian"
    ],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  }
];

games.forEach(function(game) {
    for (var prop in game) {
        console.log("property name: " + prop);
        console.log("property value: " + game[prop]);
    }
});
&#13;
&#13;
&#13;

但是你有很多选择,特别是在现代环境中。例如:

for (const game of games) {
    for (const [prop, value] of Object.entries(game)) {
        console.log(`${prop} is ${value}`);
    }
}

...使用for-of,解构和Object.entries

直播示例:

&#13;
&#13;
var games = [{
    "name": "PCH Mystery Game",
    "maxScore": 50000,
    "levels": 4,
    "players": ["akshat", "joe", "dandan", "andrew"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Token Dash",
    "maxScore": 11500,
    "levels": 2,
    "players": ["andrew", "dandan", "matt", "mitchell", "hadi"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Balloon Drop",
    "maxScore": 500,
    "levels": 1,
    "players": [
      "akshat", "joe", "dandan", "dan", "mark", "nagesh", "jessie", "lou"
    ],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Envelope Toss",
    "maxScore": 7000,
    "levels": 84,
    "players": ["akshat", "jessie", "joe", "andrew", "dandan"],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  },
  {
    "name": "PCH Prize Patrol Race",
    "maxScore": 100000,
    "levels": 5,
    "players": [
      "akshat", "joe", "andrew", "dandan", "lou", "roberto", "jessie", "haim", "matt", "mitchell", "ian"
    ],
    "maps": {
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4
    }
  }
];

for (const game of games) {
    for (const [prop, value] of Object.entries(game)) {
        console.log(`${prop} is ${value}`);
    }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

for(prop in games[game])更清晰

for (i in games) {
  var game = games[i];
  document.write("game: " + game.name + "<br>");
  for (prop in game) {
    document.write("property name: " + prop + "<br>");
    document.write("property value: " + game[prop] + "<br>");
  }
  document.write("<br>");
}

请注意,for...in通常不会在数组中使用,因为数组通常需要有一个订单而且for...in不一定会尊重订单。它还有一些按键,但for (i = 0; i < games.length; i++)没有任何问题,然后以同样的方式继续。