javascript find object in array by object property code example
Example 1: js get array item by property
const jsObjects = [
{id: 1, displayName: "First"},
{id: 2, displayName: "Second"},
{id: 3, displayName: "Third"},
{id: 4, displayName: "Fourth"}
]
var result = jsObjects.find(obj => {
return obj.id === 1
})
console.log(result)
Example 2: javascript find object in array
myArray.findIndex(x => x.id === '45');
Example 3: js find element in array by property
var result = jsObjects.find(obj => {
return obj.b === 6
})
Example 4: search an array of objects with specific object property value
var result = jsObjects.find(obj => {
return obj.b === 6
});
Example 5: return object list in find js
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function isCherries(fruit) {
return fruit.name === 'cherries' && fruit.name === 'bananas';
}
console.log(inventory.find(isCherries));