javascript find object in array based on property value code example
Example 1: javascript find object by property in array
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
Example 2: js find element in array by property
var result = jsObjects.find(obj => {
return obj.b === 6
})
Example 3: javascript get array object by id
myArray.find(x => x.id === '45').foo;
Example 4: javascript find object in array by property value
const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
const filterItems = (needle, heystack) => {
let query = needle.toLowerCase();
return heystack.filter(item => item.toLowerCase().indexOf(query) >= 0);
}
console.log(filterItems('ap', fruits));
console.log(filterItems('ang', fruits));
Example 5: search an array of objects with specific object property value
var result = jsObjects.find(obj => {
return obj.b === 6
});