So out of curiosity, what’s wrong with plain for loops in this case? It saves overhead on memory allocation overhead, JIT compilation, etc, and doesn’t look all that confusing to me:
totalBasket = {
let sum = 0;
for (let i = 0; i < fruits.length; i++) sum += fruits[i].quantity;
for (let i = 0; i < veg.length; i++) sum += veg[i].quantity;
return sum;
}
EDIT: Alternatively, you could try for...of
, although that is currently a bit slower in most JS engines:
totalBasket = {
let sum = 0;
for (let f of fruits) sum += f.quantity;
for (let v of veg) sum += v.quantity;
return sum;
}```