0

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'}
]

1 Answer 1

2

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.

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

Comments

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.