javascript array find object by 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 by property in array
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
Example 3: javascript get array object by id
myArray.find(x => x.id === '45').foo;
Example 4: find object in array by property javascript
const desiredObject = myArray.find(element => element.prop === desiredValue);