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

前端 未结 2 1975
梦如初夏
梦如初夏 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

    0 讨论(0)
  • 2021-01-27 12:14

    The push method returns the new length of the Array.

    MDN documentation for push

    The concat method returns a brand new array containing a combination of the two arrays.

    MDN documentation for concat

    0 讨论(0)
提交回复
热议问题