4.数组合并

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]
数组
JSRUN前端笔记, 是针对前端工程师开放的一个笔记分享平台,是前端工程师记录重点、分享经验的一个笔记本。JSRUN前端采用的 MarkDown 语法 (极客专用语法), 这里属于IT工程师。