How to iterate a JSON array in JavaScript?

I have this array type below. I want to repeat 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"} ] }; 
+85
json javascript arrays
Mar 19 '13 at 10:08
source share
9 answers

You can do this with the code below. First you get a data array using dictionary.data and assign it to a data variable. After that, you can repeat it using the regular 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 a similar approach to iterate over an array of images.

+133
Mar 19 '13 at 10:14
source share

There too (new for EcmaScript5):

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

The same approach for images

+29
Mar 19 '13 at 10:14
source share

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); } } 
+8
Mar 19 '13 at 10:16
source share

Use notation notes and / or brackets to access object properties and for loops to iterate arrays:

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

You can also iterate over arrays with for .. in loops; however, properties added to Array.prototype may be displayed, and you may not necessarily receive the elements of the array in the correct order or even in any sequential order.

+4
Mar 19 '13 at 10:19
source share
 for(index in dictionary) { for(var index in dictionary[]){ // do something } } 
+2
Mar 19 '13 at 10:14
source share
 for(var foo in dictionary){ for(var bar in dictionary[foo]){ for(var baz in dictionary[foo][bar]){ // do something... console.log(foo + ' > ' + baz + ' > ' + dictionary[foo][bar][baz]); } } } 

FYI: Arrays <-> Objects syntactically replaced in Javascript.

+1
Mar 19 '13 at 10:13
source share
 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); }); } 
0
Jul 28 '16 at 13:42 on
source share

Using a for and foreach loop

 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); } 
0
Nov 16 '17 at 12:02 on
source share

Here are all the options you have:

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])) } 
0
Jan 28 '19 at 13:01
source share



All Articles