J
J
justifycontent2021-07-04 12:56:18
JavaScript
justifycontent, 2021-07-04 12:56:18

Why profit is "1"?

Why is profit equal to "1"? After all, reduce inside itself does not return anything.

const [firstKey,  ...keys] = 'totalPrice'.split('.');

const profit = keys.reduce((val, key) => {
}, { items: [], totalPrice: 1 }[firstKey]);

console.log(profit)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-07-04
@justifycontent

Step through your code and you will immediately see that keys is an empty array
And reduce on an empty array simply returns init value without even going to the callback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question