how to find in object in 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)
Example 2: javascript find object by property in array
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
Example 3: find object in array javascript with property
let obj = objArray.find(obj => obj.id == 3);
Example 4: javascript find object in array by property value
const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
const filterItems = (needle, heystack) => {
let query = needle.toLowerCase();
return heystack.filter(item => item.toLowerCase().indexOf(query) >= 0);
}
console.log(filterItems('ap', fruits));
console.log(filterItems('ang', fruits));
Example 5: find in js
The first element that will be found by that function
const f = array1.find(e => e > 10);