find arrays code example

Example 1: js find value in array

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

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

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

Example 2: 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 3: find typescript

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

function findCherries(fruit) { 
    return fruit.name === 'cherries';
}

inventory.find(findCherries); // { name: 'cherries', quantity: 5 }

/* OR */
  inventory.filter(x => x.name === 'bananas')[0]; // { name: 'bananas', quantity:0}  

/* OR */

inventory.find(e => e.name === 'apples'); // { name: 'apples', quantity: 2 }

Example 4: find all of array which satisfy condition javascript

myArray.filter(x => x > 5)