I have a problem that is similar to the following:
let fruits = { Apples:0, Bananas:0, Oranges:0 }
var basket = ['Apples', 'Oranges', 'Bananas', 'Potatoes', 'Cucumber']
Objective: to update the count in fruits object based on the presence of the fruit in the basket array.
Note: the fruits object may contain more properties (fruits) than items in the basket array.
For this case, please assume that an item in the basket will not appear more than once.
I have come up with 2 solutions:
for (const fruit of Object.keys(fruits)) {
basket.includes(fruit) && fruits[fruit]++
}
basket.forEach( (fruit) => {
fruits.hasOwnProperty(fruit) && fruits[fruit]++
} )
What is the most effective way (in terms of performance) of solving this?