JS之reduce
2015-08-18 21:09
671 查看
以前没接触到reduce
忽然遇见了这么个题:
看看reduce
“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.___(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)
相关文章推荐
- js 触摸事件 touch
- DOM——JavaScript高级程序设计笔记(8)
- JS的继承
- 用Gson解析复杂的天气Json数据
- 图形验证码(JSP+Servlet)
- java和javascript双引号嵌套的问题
- 5款帮助简化HTML5 Audio开发的Javascript类库
- 【JS/读书随笔】JavaScript编程精解/Eloquent JavaScript:Chapter 4 错误处理
- 格式化JSON中时间
- jS数组
- 高性能JSON工具-FastJson处理超大JSON文本
- JavaScript inheritance by example
- jsp和Servlet区别
- Javascript入门
- 使用CamanJS在Web页面上处理图像的技巧
- js data日期初始化的方法
- JavaScript跨域总结与解决办法
- 未能加载文件或程序集“Newtonsoft.Json,Version=4.5.0.0 问题?
- JS实现下拉菜单赋值到文本框的方法
- 学习knockoutjs轻量级的MVVM框架