如何在JavaScript中迭代JSON数组? [英] How to iterate JSON array in JavaScript?

查看:160
本文介绍了如何在JavaScript中迭代JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类型的数组。我想在JavaScript中迭代这个数组。这怎么可能?

I have this below type of array. I want to iterate this array 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获取数据数组并将其分配给数据变量。之后,您可以使用普通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.

这篇关于如何在JavaScript中迭代JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆