js reduce方法

1.使用reduce()对数值进行累加
<script>
var arr = [1,2,3,4,5]
arr.reduce(function(prev,curr,index,array){
console.log(prev +curr);
console.log(index);
console.log(array)
return prev + curr
});
script>

2.将多个数组合并成一个数组
<script>
var arr1 = [1,2,3];
var arr2 = [4,5];
var arr3 = [6,7];
var newArr = [arr1,arr2,arr3].reduce(function(a,b){
return a.concat(b)
},[])
console.log(newArr)
script>

你可能感兴趣的:(javascript)