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

9
  • 1
    Array.find() Commented Dec 12, 2017 at 13:34
  • 2
    efficient way other than looping No. Any mechanism will use loop internally Commented Dec 12, 2017 at 13:35
  • 1
    @Satpal for efficiency, I would rather suggest using for + if + break or for + return Commented Dec 12, 2017 at 13:35
  • 2
    @Rajesh, find() breaks on first match Commented Dec 12, 2017 at 13:36
  • 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. Commented Dec 12, 2017 at 13:39

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Should we not look and close it as dupe?
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]

Comments

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)

1 Comment

Should we not look and close it as dupe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.