I am currently making a call to a service which sends a response as an array of objects with name value pairs. An example of this can be seen below. There could be any amount of these name value pairs in any order but I just want to access the value for the name "name2". Is there an efficient way other than looping through each object and checking the name to obtain the corresponding value for name2?
[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}]
So from the above data set I would want an efficient way to search for name2 and get the corresponding value "value2".
Thanks
I am currently making a call to a service which sends a response as an array of objects with name value pairs. An example of this can be seen below. There could be any amount of these name value pairs in any order but I just want to access the value for the name "name2". Is there an efficient way other than looping through each object and checking the name to obtain the corresponding value for name2?
[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}]
So from the above data set I would want an efficient way to search for name2 and get the corresponding value "value2".
Thanks
Share Improve this question asked Dec 12, 2017 at 13:33 J145J145 6991 gold badge13 silver badges17 bronze badges 9-
1
Array.find()
– Satpal Commented Dec 12, 2017 at 13:34 - 2 efficient way other than looping No. Any mechanism will use loop internally – Rajesh Commented Dec 12, 2017 at 13:35
-
1
@Satpal for efficiency, I would rather suggest using
for
+if
+break
orfor
+return
– Rajesh Commented Dec 12, 2017 at 13:35 -
2
@Rajesh,
find()
breaks on first match – Satpal Commented Dec 12, 2017 at 13:36 - 1 If only needing a single record from the results is a mon use case, you should probably get in touch with the service providing the results - they may be happy to update things at their end to filter the results to send less data to their users. – James Thorpe Commented Dec 12, 2017 at 13:39
3 Answers
Reset to default 3Unless you know the specific index of the object with the name name2
no.
You'll need to iterate until you find that name then you can bail out.
e.g.
var jsonData = [{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}];
for(var i=0;i<jsonData.length;i++){
if(jsonData[i]['name'] == 'name2'){
console.log('The value is: ' + jsonData[i]['value']);
break;
}
}
Note that if you don't need to support Internet Explorer, you can use the Array.find() method to simplify this.
if you don't want to iterate over the array manually you can use lambda expression as the following
a =[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}] ;
//get the items that match your criteria
a.filter(item=>item.name=="name2")
//get those items' values
a.filter(item=>item.name=="name2").map(item=>item.value)
//get the first value (if you know that it's only one item that will match)
a.filter(item=>item.name=="name2").map(item=>item.value)[0]
You can use Array#find
method.
var arr = [{
"name": "name1",
"value": "value1"
}, {
"name": "name2",
"value": "value2"
}];
// get the object containing name as `name2`
var obj = arr.find(function(v) {
return v.name === 'name2';
});
// get value property if object is defined
var res = obj && obj.value;
console.log(res)