Why Can't Direcrlty Return A Value By Using .push() In Javascript?
using below flatten function as example, why cant I use return directly on the accumulator. Push() function flatten(array){ return reduce(array,function(accumulator, value){
Solution 1:
The push
method returns the new length of the Array.
The concat
method returns a brand new array containing a combination of the two arrays.
Solution 2:
If you want a somewhat 'odd' solution that will mean you can return on the same line, you could do this:
return (accumulator.push(value1) + 1) && accumulator;
The +1 is to ensure an index of 0 is turned true.
Like I sad, it allows you to return on the line you want, but with odd code. Might suit you case maybe?
But it seems what you'd want to use is concat
Post a Comment for "Why Can't Direcrlty Return A Value By Using .push() In Javascript?"