gpt4 book ai didi

javascript - for/in 循环内的 for/in 循环

转载 作者:行者123 更新时间:2023-11-30 08:23:27 25 4
gpt4 key购买 nike

我很想知道您是否可以像使用标准 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>");
}

最佳答案

从根本上说,是的,您可以嵌套 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]);
}
});

实例:

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

但是您有很多选择,尤其是在现代环境中。例如:

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

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

实例:

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

关于javascript - for/in 循环内的 for/in 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49779902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com