find the individual element in an array javascript code example

Example 1: javascript find

const inventory = [
  {name: 'apples', quantity: 2},
  {name: 'cherries', quantity: 8}
  {name: 'bananas', quantity: 0},
  {name: 'cherries', quantity: 5}
  {name: 'cherries', quantity: 15}
  
];

const result = inventory.find( ({ name }) => name === 'cherries' );

console.log(result) // { name: 'cherries', quantity: 5 }

Example 2: javascript find object by property in array

// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');