JavaScript Loop Through JSON Array

How to loop through JSON array and get specific values

in your loop:

for (x in myObj) {
alert(x.car1);
}

x is the string value of key of your object. In order to get the car1 property of your nested object you can change your loop as:

for (x in myObj) {
alert(myObj[x].car1);
}

It is also a good practice to use hasOwnProperty while using for-in loop it might also iterate over properties which are in your object's prototype chain.

for (x in myObj) {
if (myObj.hasOwnProperty(x)) {
alert(myObj[x].car1);
}
}

How do I loop through a JSON array?

You can use for..in loops on objects to get the keys and iterate over them. Objects don't implement the Iterable interface, so for...of doesnt work. And forEach is not a method on an object, but on an array.

const json = {
"channels": {
"main": {
"id": "691548147922763825"
},
"creations": {
"id": "700390086390448148"
},
"fanart": {
"id": "691551615873843211"
},
"memes": {
"id": "691549417173680148"
}
}
}

for( let key in json.channels ) {
console.log( key, json.channels[key] )
}

Loop through JSON ARRAY and get value with node.js

In mean time I found a very easy way to get value from json array as follow:

Important NOTE (especially for the beginners):

If you are receiving response with http request, response is coming as a text string, so you need first to convert to JSON object as be abblle to extract values from array.

Full sample of the response is written on beginig of this thread.

http request code 
...
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
const response = JSON.parse(body.toString()); //convert string to JSON object

var result = response.originalDetectIntentRequest.payload.contact.cId;
console.log(result);
return result;
});

OR:

...
var result = response.queryResult.outputContexts[0].name;
console.log(result);
return result;
});

How do I iterate over a JSON structure?

Taken from jQuery docs:

var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };

jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});

jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});

Count and loop through JSON object of arrays

I got it to work by using the following:

var codes = require('./nl.json');

codes.forEach((item) => {
var areaCodeTest = item.areaCode;
var areaNameTest = item.areaName;

it("and search for postal code ", function(){
var postCode = element(by.id("imysearchstring"));
postCode.click();
console.log(areaCodeTest);
postCode.clear().sendKeys(areaCodeTest);
browser.sleep(1000);
console.log("Typed " + areaCodeTest);
});
}

I am not a 100% what the => means near the foreach but I am currently researching why my code works. If you know please post a comment so that other developers also learn.

This let me think of the meme "not sure why code does not work / Not sure why code works"

How to loop through JSON array?

You need to access the result object on iteration.

for (var key in result)
{
if (result.hasOwnProperty(key))
{
// here you have access to
var MNGR_NAME = result[key].MNGR_NAME;
var MGR_ID = result[key].MGR_ID;
}
}

Looping through objects in JSON array

Using for..in to iterate through arrays generally isn't a great idea in JS. Try using Array.forEach to go through all items in the array instead:

var obj = workouts[i].exercises;
obj.forEach(function(exercise) {
alert(exercise.name);
});


Related Topics



Leave a reply



Submit