10

I have a variable which holds this data:

{"main":[
  {"id":"123","name":"name 1"},
  {"id":"234","name":"name 2"}

  ]
}

I know the id of the data I want to search.

My question is...How do I search for the name of id 234 (for example) is thw data above?

2
  • Use JSON.parse(). Commented Sep 6, 2016 at 11:40
  • iterate in it with for and compare each record. Commented Sep 6, 2016 at 11:42

4 Answers 4

10

Use Array#filter

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

var object = {
  "main": [{
    "id": "123",
    "name": "name 1"
  }, {
    "id": "234",
    "name": "name 2"
  }]
};
var toFind = "234";
var filtered = object.main.filter(function(el) {
  return el.id === toFind;
});
console.log(filtered);

If there is only one object in the array, for-loop with break could be preferred.

var object = {
  "main": [{
    "id": "123",
    "name": "name 1"
  }, {
    "id": "234",
    "name": "name 2"
  }]
};
var toFind = "234";
for (var i = 0, len = object.main.length; i < len; i++) {
  if (object.main[i].id === toFind) {
    break;
  }
}
console.log(object.main[i].name);

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

4 Comments

why use filter? it will iterate over all array
@pwolaq – OP may have more than one object having similar ID...
OP said that he wants to get a name, not array of objects with names
@pwolaq – Not sure who is correct, I have added another approach as well... I hope it covers your point as well...
2

You can use find() if id's are unique.

var data = {"main":[
  {"id":"123","name":"name 1"},
  {"id":"234","name":"name 2"}
  ]
}

var name = data.main.find(function(o) {
  return o.id == '234';
}).name;

console.log(name)

2 Comments

find is part of ES6
not to mention it will cause an error if there is no element with given id
2

In ES5 environment, you could use Array#some, if you expect only one result.

var data = { "main": [{ "id": "123", "name": "name 1" }, { "id": "234", "name": "name 2" }] },
    result;

data.main.some(function (a) {
    if (a.id === '234') {
        result = a;
        return true;
    }
});

console.log(result);

When you expect more than one result set, you may better use Array#filter

var data = { "main": [{ "id": "123", "name": "name 1" }, { "id": "234", "name": "name 2a" }, { "id": "234", "name": "name 2" }] },
    result = data.main.filter(function (a) {
        return a.id === '234';
    });

console.log(result);

In ES6 environment, you could use Array#find for the first found element, but if there are more to find, then use Array#filter.

var data = { "main": [{ "id": "123", "name": "name 1" }, { "id": "234", "name": "name 2" }] },
    result = data.main.find(a => a.id === '234');

console.log(result);

Comments

0

You can use Array.prototype.filter to find all matching elements and return them as a new array.

To find just the first match, you can use Array.prototype.find (ES2015+).

Comments