1.相同点:

  • 都会循环遍历数组中的每一项;
  • map()、forEach()和filter()方法里每次执行匿名函数都支持3个参数,参数分别是:当前元素、当前元素的索引、当前元素所属的数组;
  • 匿名函数中的this都是指向window;
  • 只能遍历数组。

2.不同点:

  • map()速度比forEach()快;
  • map()和filter()会返回一个新数组,不对原数组产生影响;forEach()不会产生新数组,返回undefined;reduce()函数是把数组缩减为一个值(比如求和、求积);
  • reduce()有4个参数,第一个参数为初始值。

3.forEach使用

var array1 = ['a', 'b', 'c'];

array1.forEach(function(element) {
console.log(element);
});

4.map使用

var array1 = [, , , ];

// pass a function to map
const map1 = array1.map(x => x * ); console.log(map1);
// expected output: Array [2, 8, 18, 32]

5.filter使用

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > );

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

6.reduce使用

const array1 = [, , , ];
const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10 // 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, ));
// expected output: 15

forEach、map、filter、reduce的区别的更多相关文章

  1. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  2. 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight

    做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...

  3. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  4. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  5. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

  6. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  7. python中filter、map、reduce的区别

    python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...

  8. lambda,map,filter,reduce

    lambda 编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数.返回一个函数对象. func = lambda x,y:x+y fu ...

  9. for循环,foreach, map,reduce用法对比+for in,for of

    for不做赘述,相当简单: foreach方法: forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数. 注意: forEach() 对于空数组是不会执行回调函数的. array.f ...

  10. Python map filter reduce enumerate zip 的用法

    map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...

随机推荐

  1. 深入理解java:4. 框架编程

    了解 Servlet 和 Filter Servlet(即servlet-api.jar) 是 J2EE 最重要的一部分,有了 Servlet 你就是 J2EE 了,J2EE 的其他方面的内容择需采用 ...

  2. jinja2模板接受

    from flask import Flask,render_template app = Flask(__name__)#template_folder='templates',默认就是templa ...

  3. flaskurl传参用法

    from flask import Flask,request app = Flask(__name__) @app.route("/") def index(): return ...

  4. 关于Eclipse及JDK安装过程中的一些问题

    一,环境变量的配置 1.配置CLASSPATH系统变量 CLASSPATH系统变量为类查找路径 ①.在使用javac进行编译时遇到import时候就会通过这个变量里面配置的路径去查找.如果配置的是目录 ...

  5. 第十周java总结

    Java IO 1.file类 file类的构造方法: public Flie(String pathname) -->实例化Flie类的时候,必须设置好路径. 如:Flie f = new F ...

  6. Linux中安装配置KVM虚拟化

    KVM 概述: KVM 即 Kernel-based Virtual Machine 基于内核的虚拟机. KVM,是一个开源的系统虚拟化模块,自 Linux 2.6.20 之后集成在 Linux 的各 ...

  7. 【转帖】AMD:未向合资企业THATIC发放后续芯片设计授权

    AMD:未向合资企业THATIC发放后续芯片设计授权 https://www.cnbeta.com/articles/tech/854193.htm 海光和兆芯的CPU 都不靠谱啊. 在台北电脑展(C ...

  8. 头大!RabbitMQ 和 Kafka 到底怎么选?

    前言 开源社区有好多优秀的队列中间件,比如RabbitMQ和Kafka,每个队列都貌似有其特性,在进行工程选择时,往往眼花缭乱,不知所措. 对于RabbitMQ和Kafka,到底应该选哪个? Rabb ...

  9. PHP post调接口代码

    PHP post调接口代码 /** * $url:接口地址 * $data:数组参数 **/ function postData($url, $data) { $ch = curl_init (); ...

  10. java延时队列

    应用场景 1)7天自动收货 a.用户支付完成以后,把订单ID插入到内存的一个DelayQueue中,同时插入到Redis中. b.7天之内,用户点击了确认收货,则从DelayQueue中删除,从Red ...