2

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

J145
  • 607
  • 1
  • 11
  • 16
  • 1
    [`Array.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Satpal Dec 12 '17 at 13:34
  • 2
    *efficient way other than looping* No. Any mechanism will use loop internally – Rajesh Dec 12 '17 at 13:35
  • If you need just one value out of the entire array, the most efficient way is to go through the array (either 'manually' or using Array.find()). If you need more values, it might be worth it to extract the values into an object first, then look-up from that object. – xs0 Dec 12 '17 at 13:35
  • 1
    @Satpal for efficiency, I would rather suggest using `for` + `if` + `break` or `for` + `return` – Rajesh Dec 12 '17 at 13:35
  • 2
    @Rajesh, `find()` breaks on first match – Satpal Dec 12 '17 at 13:36
  • @Satpal I'm aware what find does, my point was, find is more expensive when it comes to big arrays. – Rajesh Dec 12 '17 at 13:39
  • 1
    If only needing a single record from the results is a common 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 Dec 12 '17 at 13:39
  • Not sure but possible duplicate: https://stackoverflow.com/questions/10457264/how-to-find-first-element-of-array-matching-a-boolean-condition-in-javascript – Rajesh Dec 12 '17 at 13:40
  • 1
    Possible duplicate of [Search an array for matching attribute](https://stackoverflow.com/questions/2166765/search-an-array-for-matching-attribute) – Rajesh Dec 12 '17 at 13:41

3 Answers3

3

Unless 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.

scunliffe
  • 62,582
  • 25
  • 126
  • 161
2

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]
mina sameh
  • 1,069
  • 7
  • 20
1

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)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188