I have an array of numbers [22, 44, 12, 9, 4, 23, 1, 11, 10, 5, 2, 123], I need to use reduce to create an object that looks like this:
{
numbersLessThanTen: [...],
numbersGreaterThanTen: [...]
}
I have the solution, which is the below:
const listOfNumbers = [22, 44, 12, 9, 4, 23, 1, 11, 10, 5, 2, 123];
const groupedBySize = listOfNumbers.reduce((total, next) => {
const less = total.numbersLessThanTen || [];
const more = total.numbersGreaterThanTen || [];
next > 10 ? total.numbersGreaterThanTen = [].concat(more, next) : total.numbersLessThanTen = [].concat(less, next);
return total;
}, {});
My question is, why does the following not work? It just returns the initial value. It works when I use .push() instead of .concat() but I really want to understand why this way does not work. Thank you!
const groupedBySize = listOfNumbers.reduce((total, next) => {
// const less = total.numbersLessThanTen || [];
// const more = total.numbersGreaterThanTen || [];
next > 10 ? total.numbersGreaterThanTen.concat(next) : total.numbersLessThanTen.concat(next);
return total;
}, {numbersGreaterThanTen: [], numbersLessThanTen:[]});