on developer.mozilla i found an example of working with array find:
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function isCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }
I have an cart Class with an array of objects, so, i am having a function checkQuantity which should return whatever item satisfies the condition. Also i am having same function where i need to find.
i tried to implement this approach from mozilla, and i did like this:
itemSearch(item) {
return item.id === this.id &&
item.color === this.color &&
item.size === this.size
} // method which i need
and i am using it like this:
checkQuantity() {
return this._cart.find(this.itemSearch()).quantity < this.stockCount();
}
Where i obtain undefined, however i know for sure it must find , because if i use .find(element => conditions) instead of that method, it works.
so, my Question is why it does not work? Sorry for bad english.
.find(isCherries), not.find(isCherries())thisexplicitly:this._cart.find(this.itemSearch.bind(this)). Otherwise you lose the correct context andthiswill point towindowinstead.