96

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

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

9 回答 9

143

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

var data = dictionary.data;

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

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

于 2013-03-19T10:14:21.623 回答
30

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

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

图像的相同方法

于 2013-03-19T10:14:56.763 回答
8

像这样的东西:

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);
  }
}
于 2013-03-19T10:16:45.977 回答
4

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

var d, i;

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

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

于 2013-03-19T10:19:19.863 回答
3

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

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

于 2013-03-19T10:13:10.217 回答
2
for(index in dictionary) {
 for(var index in dictionary[]){
    // do something
  }
}
于 2013-03-19T10:14:36.600 回答
0
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);
    });
}
于 2016-07-28T13:42:29.770 回答
0

使用 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);
}
于 2017-11-16T12:02:28.570 回答
0

这是您拥有的所有选项:

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)

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)

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

于 2019-01-28T13:01:12.857 回答