如何在JavaScript中迭代对象?
问题描述:
我有这个对象.我想在JavaScript中迭代此对象.这怎么可能?
I have this object. I want to iterate this object in JavaScript. How is this possible?
var dictionary = {
"data": [
{"id":"0","name":"ABC"},
{"id":"1","name":"DEF"}
],
"images": [
{"id":"0","name":"PQR"},
{"id":"1","name":"xyz"}
]
};
答
您可以使用以下代码进行操作.您首先使用dictionary.data获取数据数组,并将其分配给data变量.之后,您可以使用普通的for循环对其进行迭代.每行将是数组中的行对象.
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.
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.