定义:

reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。对空数组是不会执行回调函数的。

案例
  1. 计算数组总和
var num = [1,2,3,4,5];
var res = num.reduce(function(total,num){
return total+num;
//return total + Math.round(num);//对数组元素四舍五入并计算总和
},0);
console.log(res);//
//num.reduce((total,num) => total += num, 0);
//没有初始值initialValue(即上面例子中的0),当数组为0时会抛出异常提示reduce函数没有初始值,所以为兼容性一般加上initialValue
  1. 合并二维数组
var red = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
}, []);
console.log(red)
VM291:4 (6) [0, 1, 2, 3, 4, 5]
  1. 统计一个数组中有多少个不重复的单词:
不用reduce时:
var arr = ["apple","orange","apple","orange","pear","orange"];
function getWordCnt(){
var obj = {};
for(var i= 0, l = arr.length; i< l; i++){
var item = arr[i];
obj[item] = (obj[item] +1 ) || 1;
}
return obj;
}
console.log(getWordCnt());
VM3704:14 {apple: 2, orange: 3, pear: 1} 用reduce时:
var arr = ["apple","orange","apple","orange","pear","orange"];
function getWordCnt(){
return arr.reduce(function(prev,next){
prev[next] = (prev[next] + 1) || 1;
return prev;
},{});
}
console.log(getWordCnt());
VM3704:14 {apple: 2, orange: 3, pear: 1}
  1. 对reduce的理解:
    reduce(callback,initiaValue)会传入两个变量,回调函数(callback)和初始值(initiaValue)。
    假设函数有4个传入参数,prev和next,index和array。 Prev和next是你必须要了解的。
    当没有传入初始值时,prev是从数组中第一个元素开始的,next数组是第二个元素。
    但是当传入初始值(initiaValue)后,第一个prev将是initivalValue,next将是数组中的第一个元素。
    比如:
var arr = ["apple","orange"];
function noPassValue(){
return arr.reduce(function(prev,next){
console.log("prev:",prev);
console.log("next:",next);
return prev;
});
} function passValue(){
return arr.reduce(function(prev,next){
console.log("prev:",prev);
console.log("next:",next);
prev[next] = 1;
return prev;
},{});
}
console.log("No Additional parameter:",noPassValue());
console.log("----------------");
console.log("With {} as an additional parameter:",passValue());


VM415673:4 prev: apple
VM415673:5 next: orange
VM415673:4 prev: apple
VM415673:5 next: orange
VM415673:19 No Additional parameter: apple
VM415673:20 ----------------
VM415673:13 prev: {}
VM415673:14 next: apple
VM415673:13 prev: {apple: 1}
VM415673:14 next: orange
VM415673:21 With {} as an additional parameter: {apple: 1, orange: 1}
 

JS --- reduce()函数的更多相关文章

  1. JS中的reduce函数

    海纳百川,有容乃大 定义: reduce()方法接受一个函数作为累加器,数组中的每个值(从左向右)开始缩减,最终计算为一个值.对空数组是不会执行回调函数的. 案例: 计算数组总和: var num = ...

  2. 数组中的reduce 函数理解

    第一次见到reduce 是在js 的高级程序设计中,它的意思是把一个数组减少为一个数,举的例子是数组中元素的求和.它接受一个函数作为参数,函数又有两个参数,一个是prev, 前一个值,一个是next, ...

  3. 循序渐进VUE+Element 前端应用开发(7)--- 介绍一些常规的JS处理函数

    在我们使用VUE+Element 处理界面的时候,往往碰到需要利用JS集合处理的各种方法,如Filter.Map.reduce等方法,也可以设计到一些对象属性赋值等常规的处理或者递归的处理方法,以前对 ...

  4. JS回调函数全解析教程

    转自:http://blog.csdn.net/lulei9876/article/details/8494337 自学jQuery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速g ...

  5. 学习js回调函数

    <!DOCTYPE HTML> <html> <head> <meta charset="GBK" /> <title> ...

  6. 如何理解JS回调函数

    1.回调函数英文解释: A callback is a function that is passed as an argument to another function and is execut ...

  7. reduce() 函数

    reduce()函数 reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传 ...

  8. Atitit java方法引用(Method References) 与c#委托与脚本语言js的函数指针

    Atitit java方法引用(Method References) 与c#委托与脚本语言js的函数指针   1.1. java方法引用(Method References) 与c#委托与脚本语言js ...

  9. 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数

    来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...

随机推荐

  1. Laravel 的计划任务

    避免并发执行 $schedule->command('emails:send')->withoutOverlapping(); 这里需要注意,对于 call function 定义的计划任 ...

  2. Java编程的逻辑 (16) - 继承的细节

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  3. appium入门级教程(3)—— 安装 Android SDK

    前言 搭建Android平台不是必须的,如果你不想使用 Android 模拟器运行测试的话可以跳过,不过,建议安装:原生 Android 好折腾!关键是它自带的一些工具是做 appium 测试必须要用 ...

  4. Android uses-permission 权限大全

    Android uses-permission 权限大全 当编写某些程序时,你需要调用手机的某些功能 这时候你一定要记得启用相关的uses-permission, 很多网上贴出来的代码都不包含 Man ...

  5. intellj idea show "run dashboard" panel

    workspace.xml edit this point <component name="RunDashboard"> <option name=" ...

  6. php实现https(tls/ssl)双向认证

    php实现https(tls/ssl)双向认证 通常情况下,在部署https的时候,是基于ssl单向认证的,也就是说只要客户端认证服务器,而服务器不需要认证客户端. 但在一些安全性较高的场景,如银行, ...

  7. Angular 5项目

    Angular 5项目 如果您正在使用angular, 但是没有好好利用angular cli的话, 那么可以看看本文. Angular CLI 官网: https://github.com/angu ...

  8. 1044-Access denied for user 'root'@'%' to database 'lc_db'

    远程登录Linux中的MySQL时,如果直接在工具中创建数据库时,有可能出现下面图中这样的错误: 这种错误是在远程登录时造成的,如果直接在Linux中本地操作没有问题(在Linux中的MySQL下,通 ...

  9. Java中实现多线程的两种方式之间的区别

    Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...

  10. 修改无线wifi网络名称。注册表。windows 无线属性 windows 无线 配置文件

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha windows 无线属性 windows 无线 配置文件 ======= 修改完成,之后 ...