关于数组:如何在JavaScript中迭代对象?

How to iterate object in JavaScript?

本问题已经有最佳答案,请猛点这里访问。

我有这个东西。我想用JavaScript迭代这个对象。这怎么可能?

1
2
3
4
5
6
7
8
9
10
var dictionary = {
   "data": [
        {"id":"0","name":"ABC"},
        {"id":"1","name":"DEF"}
    ],
   "images": [
        {"id":"0","name":"PQR"},
        {"id":"1","name":"xyz"}
    ]
};

You can do it with the below code. You first get the data array using dictionary.data and assign it to the data variable. After that you can iterate it using a normal for loop. Each row will be a row object in the array.

1
2
3
4
5
6
7
var data = dictionary.data;

for (var i in data)
{
     var id = data[i].id;
     var name = data[i].name;
}

您可以使用类似的方法迭代图像数组。


也有这样的方法(ECMAScript5的新方法):

1
2
3
dictionary.data.forEach(function(item){
    console.log(item.name + ' ' + item.id);
});

同样的图像处理方法


像这样:

1
2
3
4
5
6
7
8
9
10
11
12
var dictionary = {"data":[{"id":"0","name":"ABC
<div class="
suo-content">[collapse title=""]<ul><li>for…in不适用于数组。-所以对于…在字典中,但是对于(var j=0;j<dictionary[i].length;j++)…</li></ul>[/collapse]</div><hr>
<p>
Use dot notation and/or bracket notation to access object properties and <wyn>for</wyn> loops to iterate arrays:
</p>

[cc lang="
javascript"]var d, i;

for (i = 0; i < dictionary.data.length; i++) {
  d = dictionary.data[i];
  alert(d.id + ' ' + d.name);
}

您还可以使用for..in循环迭代数组;但是,添加到Array.prototype的属性可能会显示出来,并且您可能不一定以正确的顺序或任何一致的顺序获得数组元素。


1
2
3
4
5
for(index in dictionary) {
 for(var index in dictionary[]){
    // do something
  }
}


使用一个生成器函数,您可以迭代深层键值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function * deepEntries(obj) {
    for(let [key, value] of Object.entries(obj)) {
        if (typeof value !== 'object')
            yield [key, value]
        else
            for(let entries of deepEntries(value))
                yield [key, ...entries]
    }
}

const dictionary = {
   "data": [
        {"id":"0","name":"ABC
<p><center>[wp_ad_camp_2]</center></p><hr><P>以下是您拥有的所有选项:</P>1。<wyn>for...of</wyn>号(ES2015)<P>[cc lang="
javascript"]var dictionary = {
   "
data": [
        {"
id":"0","name":"ABC"},
        {"
id":"1","name":"DEF"}
    ],
   "
images": [
        {"
id":"0","name":"PQR"},
        {"
id":"1","name":"xyz"}
    ]
};

for (const entry of dictionary.data) {
  console.log(JSON.stringify(entry))
}

2。Array.prototype.forEach号(ES5)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var dictionary = {
   "data": [
        {"id":"0","name":"ABC"},
        {"id":"1","name":"DEF"}
    ],
   "images": [
        {"id":"0","name":"PQR"},
        {"id":"1","name":"xyz"}
    ]
};

dictionary.data.forEach(function(entry) {
  console.log(JSON.stringify(entry))
})

三。for()号(ES1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var dictionary = {
   "data": [
        {"id":"0","name":"ABC"},
        {"id":"1","name":"DEF"}
    ],
   "images": [
        {"id":"0","name":"PQR"},
        {"id":"1","name":"xyz"}
    ]
};

for (let i = 0; i < dictionary.data.length; i++) {
  console.log(JSON.stringify(dictionary.data[i]))
}


Using for and foreach loop

1
2
3
4
5
6
7
8
9
10
11
var dictionary = {
     data: [{ id:"0", name:"ABC" }, { id:"1", name:"DEF" }],
     images: [{ id:"0", name:"PQR" }, { id:"1", name:"xyz" }]
};
dictionary.data.forEach(item => {
     console.log(item.id +"" + item.name);
});

for (var i = 0; i < dictionary.data.length; i++) {
     console.log(dictionary.data[i].id +"" + dictionary.data[i].name);
}

[cc lang="javascript"]var dictionary = {
"data":[{"id":"0","name":"ABC