Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  166] [ 1]  / answers: 1 / hits: 76659  / 9 Years ago, tue, december 29, 2015, 12:00:00

I have an object with key value pairs inside an array:



var data = [
{
errorCode:100,
message:{},
name:InternetGatewayDevice.LANDevice.1.Hosts.HostNumberOfEntries,
value:2
}
];


I want to get the value of value key in the object. ie, the output should be 2.



I tried this:



console.log(data[value]);
console.log(data.value);


Both logging undefined. I saw similar questions in SO itself. But, I couldn't figure out a solution for my problem.


More From » arrays

 Answers
21

You are trying to get the value from the first element of the array. ie, data[0]. This will work:


console.log(data[0].value);

If you have multiple elements in the array, use JavaScript map function or some other function like forEach to iterate through the arrays.


data.map(x => console.log(x.value));

data.forEach(x => console.log(x.value));

[#63911] Saturday, December 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristopherh

Total Points: 402
Total Questions: 117
Total Answers: 84

Location: Monaco
Member since Fri, Sep 24, 2021
3 Years ago
;