admin管理员组文章数量:1406009
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)
本文标签: Efficient way to find name value pairs from JSON array in JavascriptStack Overflow
版权声明:本文标题:Efficient way to find name value pairs from JSON array in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744374358a2603184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论