Very quick and easy summation computations console.log(newArr); // [0, 1, 2, 3, 4, 5]

array.reduce(function(Total, currentValue, currentIndex, ARR), initialValue) Total Required. The initial value, or the return value at the end of the calculation. CurrentValue required. The current element currentIndex is optional. The index ARR of the current element is optional. The array object to which the current element belongs. The initialValue is optional. Const array = [{a: 1, num: 100}, {b: 2, num: 200}]; const total = array.reduce((prev, cur) => prev + cur.num, 0); console.log(total); // 300 const arr = [1,2,3,4] const mul = array.reducy((x,y) => x*y) console.log(mul); / / 24# 2d array conversion
let arr = [[0, 1], [2, 3], [4, 5]]
let newArr = arr.reduce((pre,cur)=>{    
    return pre.concat(cur)
},[])
console.log(newArr); // [0, 1, 2, 3, 4, 5]

Convert a multidimensional array to a one-dimensional array
letArr = [[0, 1], [2, 3], [4, [5, 6]]] const newArr =function(arr){
   returnarr.reduce((pre,cur)=>pre.concat(Array.isArray(cur)? newArr(cur):cur),[]) } console.log(newArr(arr)); //[0, 1, 2, 3, 4, 5, 6, 7]Copy the code