Reputation: 13
I want to get two values from my JSON array. It looks like that:
I need to get a ID and key from these arrays. Please help me, thanks. I am using newest node js.
Upvotes: 0
Views: 4661
Reputation: 192
ES6 syntax.
JSON.parse(data).map((item) => { item.id , item.key })
ES5
JSON.parse(data).map(function(item){ return {item.id , item.key }})
Upvotes: 2
Reputation: 45500
Loop through it like this:
var jsonData = JSON.parse(data);
for(var myobject in jsonData){
console.log("Id =" + myobject.id);
console.log("key = " + myobject.key);
}
or like this:
var jsonData = JSON.parse(data);
for(i = 0; i < jsonData.length; i++){
console.log("Id =" + jsonData[i].id);
console.log("key = " + jsonData[i].key);
}
Upvotes: 1
Reputation: 4597
use map() function it will return te id and the key
var id = data.map(function(par){
return "id id :" +par.id+" key is: "+ par.key;
});
working see jsfiddle
Or you can is just a loop to access each key and id
for(i = 0; i < data.length; i++){
console.log("Id is :" + data[i].id+"key is : " + data[i].key);
}
Upvotes: 0
Reputation: 263
var val1 = arr[0].id;
var k1 = arr[0].key;
var val2 = arr[1].id;
var k2 = arr[1].key;
To get the array lenght use arr.length
Upvotes: 0