1// To find a specific object in an array of objects
2myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
1const simpleArray = [3, 5, 7, 15];
2const objectArray = [{ name: 'John' }, { name: 'Emma' }]
3
4console.log( simpleArray.find(e => e === 7) )
5// expected output 7
6
7console.log( simpleArray.find(e => e === 10) )
8// expected output undefined
9
10console.log( objectArray.find(e => e.name === 'John') )
11// expected output { name: 'John' }
1const inventory = [
2 {name: 'apples', quantity: 2},
3 {name: 'cherries', quantity: 8}
4 {name: 'bananas', quantity: 0},
5 {name: 'cherries', quantity: 5}
6 {name: 'cherries', quantity: 15}
7
8];
9
10const result = inventory.find( ({ name }) => name === 'cherries' );
11
12console.log(result) // { name: 'cherries', quantity: 5 }
1//The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
2const array1 = [5, 12, 8, 130, 44];
3
4const found = array1.find(element => element > 10);
5
6console.log(found);
7// expected output: 12
1const array1 = [5, 12, 8, 130, 44];
2
3const found = array1.find(element => element > 10);
4
5console.log(found);
6// expected output: 12