如何在 JavaScript 中迭代对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15496508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:59:32  来源:igfitidea点击:

How to iterate object in JavaScript?

javascriptarrays

提问by Piyush

I have this object. I want to iterate this object in JavaScript. How is this possible?

我有这个对象。我想在 JavaScript 中迭代这个对象。这怎么可能?

var dictionary = {
    "data": [
        {"id":"0","name":"ABC"},
        {"id":"1","name":"DEF"}
    ],
    "images": [
        {"id":"0","name":"PQR"},
        {"id":"1","name":"xyz"}
    ]
};

回答by kavin

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.

您可以使用以下代码来完成。您首先使用 dictionary.data 获取数据数组并将其分配给数据变量。之后,您可以使用普通的 for 循环对其进行迭代。每一行都是数组中的一个行对象。

var data = dictionary.data;

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

You can follow similar approach to iterate the image array.

您可以按照类似的方法来迭代图像数组。

回答by Arno 2501

There's this way too (new to EcmaScript5):

也有这种方式(EcmaScript5 的新手):

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

Same approach for images

图像的相同方法

回答by happyCoda

Something like that:

类似的东西:

var dictionary = {"data":[{"id":"0","name":"ABC"},{"id":"1", "name":"DEF"}], "images": [{"id":"0","name":"PQR"},{"id":"1","name":"xyz"}]};

for (item in dictionary) {
  for (subItem in dictionary[item]) {
     console.log(dictionary[item][subItem].id);
     console.log(dictionary[item][subItem].name);
  }
}

回答by PleaseStand

Use dot notation and/or bracket notation to access object properties and forloopsto iterate arrays:

使用点符号和/或括号符号来访问对象属性和for循环来迭代数组:

var d, i;

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

You can also iterate arrays using for..inloops; however, properties added to Array.prototypemay show through, and you may not necessarily get array elements in their correct order, or even in any consistent order.

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

回答by Emissary

Using a generator function you could iterate over deep key-values.

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

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"},
        {"id":"1","name":"DEF"}
    ],
    "images": [
        {"id":"0","name":"PQR"},
        {"id":"1","name":"xyz"}
    ]
}

for(let entries of deepEntries(dictionary)) {
    const key = entries.slice(0, -1).join('.')
    const value = entries[entries.length-1]
    console.log(key, value)
}

回答by Sankalp Mishra

for(index in dictionary) {
 for(var index in dictionary[]){
    // do something
  }
}

回答by shobhit kumar

var dictionary = {
        "data":[{"id":"0","name":"ABC"}, {"id":"1","name":"DEF"}],
        "images": [ {"id":"0","name":"PQR"},"id":"1","name":"xyz"}]
};


for (var key in dictionary) {
    var getKey = dictionary[key];
    getKey.forEach(function(item) {
        console.log(item.name + ' ' + item.id);
    });
}

回答by Nikhil

Using for and foreach loop

使用 for 和 foreach 循环

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

回答by connexo

Here's all the options you have:

这是您拥有的所有选项:

1. for...of(ES2015)

1. for...of(ES2015)

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)

2. Array.prototype.forEach(ES5)

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

3. for()(ES1)

3. for()(ES1)

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