JS之reduce

来源:互联网 发布:淘宝详情页的尺寸 编辑:程序博客网 时间:2024/05/20 01:33

以前没接触到reduce
忽然遇见了这么个题:

martix = [[1,2],[3,4][5,6]]; var fl = martix.___(function(a,b)____); console.log(fl)//输出[1,2,3,4,5,6]

看看reduce

[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array) {  return previousValue + currentValue;});

“MDNreduce”
reduce的callback含四个参数 previousValue, currentValue, index, array
reduce的作用
If initialValue is provided in the call to reduce, then previousValue will be equal to initialValue and currentValue will be equal to the first value in the array. If no initialValue was provided, then previousValue will be equal to the first value in the array and currentValue will be equal to the second.
有初始值,将初始值作为previousValue, 数组第0个值作为currentValue,以此类推
无初始值,将第0个值作为previousValue,数组第1个值作为currentValue,以此类推
现在懂了

martix = [[1,2],[3,4][5,6]]; var fl = martix.reduce(function(a,b){ return a.concat(b);}); console.log(fl)
0 0
原创粉丝点击