Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question