How would you either update inventory
(based on name) or add if name not found.
var inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
For example, the following will update the inventory:
const fruit = {name: 'bananas', quantity: 1}
inventory = inventory.map(f => f.name===fruit.name ? fruit : f);
and this could be used to add to the inventory:
const fruit = {name: 'oranges', quantity: 2}
if (!inventory.find(f => f.name===fruit.name)) inventory.push(fruit)
but I'm looking for a solution which can do both and which preferably uses arrow functions rather than indexes - if possible.