Javascript中提供了多种基本的数组合并的方法
reduce: builds up a value by repeated calling the iterator, passing in previous values; see the spec for the details; useful for summing the contents of an array and many other things
reduceRight: like reduce, but works in descending rather than ascending order
实际解决问题一 :二维数组转一维数组,即数组的扁平化
var s = [[1,2],[3,4],[5,6]];
var r = s.reduce(function (p, c) {
return p.concat(c);
});
console.log(r);
// [ 1, 2, 3, 4, 5, 6 ]
注解:reduce的callback中,p指代上一个元素,c指代当前元素。
第一次迭代:p -- > [1,2] c -- > [3,4]
第二次迭代:p -- > [1,2,3,4] c -- > [5,6]