mdn array find 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 array.find

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

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

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

Example 3: js array get index

var fruits = ["Banana", "Orange", "Apple", "Mango"];
return fruits.indexOf("Apple"); // Returns 2

Example 4: array find

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

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

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

Example 5: js array find

var ages = [3, 10, 18, 20];

function checkAdult(age) {
  return age >= 18;
}
/* find() runs the input function agenst all array components
   till the function returns a value
*/
ages.find(checkAdult);

Example 6: JavaScript Array Methods .find()

const list = [5, 12, 8, 130, 44];
// a find metóduson belül a number egy paraméter
// azért nem szükséges köré a zárójel, mert csak egy paramétert adunk át
// minden más esetben így kell írni:
// const found = list.find((index, number) => number > index);
const found = list.find(number => number > 10);
console.log(found);
// --> 12