function find 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');
Example 3: find all of array which satisfy condition javascript
myArray.filter(x => x > 5)
Example 4: find a single element in array of objects javascript
myArray.find(x => x.id === '45').foo;
Example 5: js array find
const myArray = ["sun","moon","earth"];
const lookingFor = "earth"
console.log(myArray.find(planet => { return planet === lookingFor })
// expected output: earth
Example 6: find function in javascript
find array function