I am trying to figure out a way to do search while iterating an array. I came across find()
method.
Here is the example given:
var inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function findCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(findCherries));
// { name: 'cherries', quantity: 5 }
I need find a dynamic fruit name, but I can't figure out how to do it. In short, I am trying to do something like:
function findCherries(fruit, fruitName) {
return fruit.name === fruitName;
};
inventory.find(findCherries('cherries'))
//"true is not a function"
Is there a way to give find()
an argument and find a match base on that argument? If not, what method allows me to search an array of object dynamically?