I'm trying to use reduce to return an array like so:
var myArray = [1,2,3];
_.reduce(myArray, function (seed, item) { return seed.push(item);}, []);
I expect that it will produce an array just like myArray. Instead for the first item, seed is an array. Then for the second item, seed is a number. That causes an error and the third item is never reached.
Whats happening here?
Actually, seed.push()
does not return the modified seed
. Do the following, and it's right:
_.reduce(myArray, function (seed, item) { seed.push(item); return seed; }, []);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With