我有这个对象。我想在 JavaScript 中迭代这个对象。这怎么可能?
var dictionary = {
"data": [
{"id":"0","name":"ABC"},
{"id":"1","name":"DEF"}
],
"images": [
{"id":"0","name":"PQR"},
{"id":"1","name":"xyz"}
]
};
我有这个对象。我想在 JavaScript 中迭代这个对象。这怎么可能?
var dictionary = {
"data": [
{"id":"0","name":"ABC"},
{"id":"1","name":"DEF"}
],
"images": [
{"id":"0","name":"PQR"},
{"id":"1","name":"xyz"}
]
};
你可以用下面的代码来做。您首先使用 dictionary.data 获取数据数组并将其分配给数据变量。之后,您可以使用普通的 for 循环对其进行迭代。每行将是数组中的一个行对象。
var data = dictionary.data;
for (var i in data)
{
var id = data[i].id;
var name = data[i].name;
}
您可以按照类似的方法来迭代图像数组。
也有这种方式(EcmaScript5 的新手):
dictionary.data.forEach(function(item){
console.log(item.name + ' ' + item.id);
});
图像的相同方法
像这样的东西:
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);
}
}
使用生成器函数,您可以迭代深层键值。
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)
}
for(index in dictionary) {
for(var index in dictionary[]){
// do something
}
}
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);
});
}
使用 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);
}
这是您拥有的所有选项:
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))
}
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))
})
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]))
}