定义:

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. C++ code:低级编程

    1 C编程 所谓低级编程,是相对于面向对象或基于对象的抽象层次更高的高级编程而言,就是: (1)不用C++STL的资源库,尽量减少内在的创建.调用.分配等的开销: (2)对程序管辖的内存进行直接操作访 ...

  2. [转] offsetParent 到底是哪一个?

    正文 不同情况 没有已定位的父节点,且自身position:relative的DIV元素的offsetParent为BODY 没有已定位的父节点,且自身position: absolute的DIV元素 ...

  3. PHP SMTP邮件发送(可加附件)

    <?php /** * @param $address mixed 收件人 多个收件人/或需要设置收件人昵称时为数组 array($address1,$address1)/array(array ...

  4. Python3 简明教程学习(上)

    一.开始 Python 之旅交互模式 1.Ctrl + D 输入一个 EOF 字符来退出解释器,也可以键入 exit() 来退出 2.#!/usr/bin/env python3 中#!称为 Sheb ...

  5. 优雅的将Map转为String工具类

    import com.alibaba.fastjson.JSONObject;import org.apache.commons.lang3.StringUtils; import java.lang ...

  6. [转]c++优先队列(priority_queue)用法详解

    既然是队列那么先要包含头文件#include <queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队 优先队列具有队列的所有特性, ...

  7. clob字段超过4000转String类型

    上次提到listagg()和wm_concat()方法合并过的字段类型为clob,要是字段长度超过4000,直接使用to_char()方法转会报错. 解决方法可以在java代码中使用流的方式转化成字符 ...

  8. NOI.AC NOIP模拟赛 第二场 补记

    NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...

  9. KVM源代码解读:linux-3.17.4\include\linux\kvm_host.h

    #ifndef __KVM_HOST_H #define __KVM_HOST_H /* * This work is licensed under the terms of the GNU GPL, ...

  10. Android 9 patch 图片 (.9.png 格式图片) 的特点和制作(转)

    本文围绕 .9.png 格式图片讨论以下两个话题: 1. 该格式图片的特点 2. 制作方式 一 .9.png 格式的文件的特点 与传统的png 格式图片相比, 9.png 格式图片在图片四周有一圈一个 ...