Loading...
const total = cart.values()
.map((item) => item.price)
.reduce((sum, price) => sum + price);const total = cart.values()
.map((item) => item.price)
.reduce((sum, price) => sum + price, 0);Without an initial value, reduce() throws a TypeError on an empty cart; with 0 it returns 0.
reduce() without an initial value on an empty iterator, which throws a TypeError.reduce() is called on the iterator instead of converting it to an array first.reduce() callback get?The accumulator, the current value and a counter starting at 0. Unlike array reduce(), there is no argument with the source.
reduce() on an infinite iterator?Only after limiting it with take(), because reduce() reads values until the iterator is done.