How To Get A Specific Or Multiple Values (or Keys) From Nested Json
Solution 1:
Since data
is an array, you should first get the item - and since you only have one item - you'd use data[0]
, and then get access to the list
property like data[0].list[1]
- this will give you the second item in the list
- but since you are interested in a specific property (svalue
) of this item, you will then access it like this: data[0].list[1].svalue
.
A better approach would be to loop through the items in the data array - and then for each item, loop through the items in the list array. See @Rajesh's comment. I hope that helps;
Solution 2:
Specifically you can access it like this object.data[0].list[1].svalue
. The reason data.list
is undefined
is because data
corresponds to an array data: [{ }]
this is why we use data[0]
, but data
itself is a key in an object so before you can get to data
you need to access it. If the objects name where data
resides were object
(like I did below) then it'd be accessed like this object.data[0]
const object = {
"data": [{
"name": "name1",
"value": "value1",
"list": [{
"sname": "sname1",
"svalue": "svalue1"
}, {
"sname": "sname2",
"svalue": "svalue2"
}]
}]
}
console.log(object.data[0].list[1].svalue)
Post a Comment for "How To Get A Specific Or Multiple Values (or Keys) From Nested Json"