js aray find code example

Example 1: js find index in list

let myList = ['foo', 'bar', 'qux'];

myList.indexOf('bar');	// 1

Example 2: array find

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

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

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

Example 3: find object in array javascript with property

let obj = objArray.find(obj => obj.id == 3);

Example 4: 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));
// { name: 'cherries', quantity: 5 }