Using underscore.js can reduce array return?

I am trying to use shorthand to return an array as follows:

var myArray = [1,2,3]; _.reduce(myArray, function (seed, item) { return seed.push(item);}, []); 

I expect it to create an array just like myArray. Instead, for the first element, the seed is an array. Then for the second element, the seed is a number. This causes an error, and the third element is never reached.

What's going on here?

+4
source share
1 answer

Actually, seed.push() does not return the changed seed . Do the following and this is correct:

 _.reduce(myArray, function (seed, item) { seed.push(item); return seed; }, []); 
+13
source

Source: https://habr.com/ru/post/1392290/


All Articles