find array object javascript 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"}
]

// You can use the arrow function expression:
var result = jsObjects.find(obj => {
	// Returns the object where
	// the given property has some value 
  	return obj.id === 1
})

console.log(result)

// Output: {id: 1, displayName: "First"}

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: javascript find object array

const found = accesses.find(x => x.Resource === 'Clients');
console.log(found)

Example 5: array find

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12