Using the reduce function to return an array

后端 未结 6 1740
一向
一向 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:10

    I know this is the same answer, but I just want to show that using reduce (), the syntax can also be reduced to a single line of code using ES6:

    var store = [0,1,2,3,4];
    
    var stored = store.reduce((pV,cV) => [...pV, cV], []);
    
    console.log(stored);

提交回复
热议问题