Reputation: 1053
Is there any javascript function to search an element in the array of objects. We have sort function in javascript to sort an array of objects.
[
{name:'a', value:'1'},
{name:'b', value:'2'},
{name:'c', value:'3'},
{name:'d', value:'4'},
{name:'e', value:'5'}
]
Upvotes: 0
Views: 202
Reputation: 166041
You could use the ES5 Array.prototype.filter
method (MDN article). For example, to reduce the array to only those objects with a name
property of "a":
var result = yourArray.filter(function(elem) {
return elem.name === "a";
});
console.log(result); //[Object -> name: 'a', value: '1']
This is not supported by older browsers, but there are plenty of polyfills available for it.
Upvotes: 2