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"} ] };
assign() The Object. assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.
It takes the object that you want to loop over as an argument and returns an array containing all properties names (or keys). After which you can use any of the array looping methods, such as forEach(), to iterate through the array and retrieve the value of each property.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With