Using the reduce function to return an array

后端 未结 6 1729
一向
一向 2020-12-13 17:36

Why is it that when I want to use the push function inside the reduce function to return a new array I get an error. However, when I use the concat method inside the reduce

6条回答
  •  独厮守ぢ
    2020-12-13 18:28

    Array.prototype.push method returns the new length of the array.

    Array.prototype.concat method inserts new element into array and returns array back so it can be further processed. This is what you need to do with reduce: pass modified array the the next iteration.

提交回复
热议问题