Why can't direcrlty return a value by using .push() in javascript?

前端 未结 2 2001
梦如初夏
梦如初夏 2021-01-27 11:42

using below flatten function as example, why cant I use return directly on the accumulator. Push()

 function flatten(array){
      return reduce(arr         


        
2条回答
  •  庸人自扰
    2021-01-27 11:57

    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

提交回复
热议问题