关于数组中forEach() 、map()、filter()、reduce()、some()、every()的总结

1. forEach()

let array = [1,2,3,4];

array.forEach((item, index, array) => {

  console.log(item);

});

forEach会遍历数组, 没有返回值, 不允许在循环体内写return, 不会改变原来数组的内容.

2. map()

let array = [1, 2, 3, 4];

let temp = array.map((item, index, array) => {

  return item * 10;

});

console.log(temp);  //  [10, 20, 30, 40];

console.log(array);  // [1, 2, 3, 4]

// map 遍历数组, 会返回一个新数组, 不会改变原来数组里的内容

let temp2 = array.map(String);  // 把数组里的元素都转成字符串

3. filter()

let array = [1, 2, 3, 4];

let temp = array.filter((item, index, array) => {

  return item >  3;

});

console.log(temp);  // [4]

console.log(array);  // [1, 2, 3, 4]

// filter 会过滤掉数组中不满足条件的元素, 把满足条件的元素放到一个新数组中, 不改变原数组

4. reduce()

let array = [1, 2, 3, 4];

let temp = array.reduce((x, y) => {

  console.log("x": x);

  console.log("y": y);

  return x + y;

});

console.log(temp);  // 10

console.log(array);  // [1, 2, 3, 4]

// x 是上一次计算过的值, 第一次循环的时候是数组中的第1个元素

// y 是数组中的每个元素, 第一次循环的时候是数组的第2个元素

5. every()

let array = [1, 2, 3, 4];

let bo = array.every((item, index, array) => {

  return item > 2;

});

console.log(bo);    // false;

// every遍历数组, 每一项都是true, 则返回true, 只要有一个是false, 就返回false

6. some()

let array = [1, 2, 3, 4];

let tmep = array.some((item, index, array) => {

  return item > 1;

});

console.log(temp);  // true

// 遍历数组的每一项, 有一个返回true, 就停止循环

以上6个方法IE9及以上才支持。不过可以通过babel转义支持IE低版本。

以上均不改变原数组。

some、every返回true、false。

map、filter返回一个新数组。

reduce让数组的前后两项进行某种计算,返回最终操作的结果。

forEach 无返回值。

ES6数组方法总结的更多相关文章

  1. ES6 数组方法拓展

    ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...

  2. ES6数组方法

    ES6数组方法 以下方法添加到了Array.prototype对象上(isArray除外) indexOf 类似字符串的indexOf()方法 stringObject.indexOf(searchv ...

  3. ES5和ES6数组方法

    ES5 方法 indexOf和lastIndexOf 都接受两个参数:查找的值.查找起始位置不存在,返回 -1 :存在,返回位置.indexOf 是从前往后查找, lastIndexOf 是从后往前查 ...

  4. ES6 数组方法 forEach map filter find every some reduce

    1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(pa ...

  5. ES6数组的扩展--Array.from()和Array.of()

    一. Array.from() : 将伪数组对象或可遍历对象转换为真数组 1.何为伪数组 如果一个对象的所有键名都是正整数或零,并且有length属性,那么这个对象就很像数组,语法上称为"类 ...

  6. ES6数组及数组方法

    ES6数组可以支持下面的几种写法: (1)var [a,b,c] = [1,2,3]; (2)var [a,[[b],c]] = [1,[[2],3]]; (3)let [x,,y] = [1,2,3 ...

  7. 最新数组方法(包括es6)

    整理目前所用过的数组方法,学习了新增的es6方法. 1 arr.push() 从后面添加元素,返回值为添加完后的数组的长度 let arr = [1,2,3,4,5] console.log(arr. ...

  8. ES6新增的常用数组方法(forEach,map,filter,every,some)

    ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...

  9. es6新增的数组方法和对象

    es6新增的遍历数组的方法,后面都会用这个方法来遍历数组,或者对象,还有set,map let arr=[1,2,3,4,3,2,1,2]; 遍历数组最简洁直接的方法法 for (let value ...

随机推荐

  1. [清华集训2015]灯泡(浙江大学ZOJ 3203 Light Bulb)

    Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, his brother ...

  2. 数码视讯Q7的刷机

    Q7的硬件配置 CPU: S905LRAM: MIRA P3P4GF4DMF DDR3 512MB * 2 = 1GBROM: 镁光29F64G08CBABB * 1 = 8GBWIFI: RTL81 ...

  3. Python + Selenium 自动发布文章(一):开源中国

    https://blog.csdn.net/qq_28804275/article/details/80891949 https://blog.csdn.net/qq_28804275/article ...

  4. Go:return 与 defer的执行顺序 (转)

    将下面的代码放入了 double.go 文件内 package main //go:noinline //go:nosplit func test() (x int) { defer println( ...

  5. 由swap引出的局部变量,形参和指针的小问题

    1.第一种实现swap函数的方法是: swap(int a,int b) { Int c = a;a = b;b =c; } 这表面一看确实是实现了整数a,b的交换,当拿来用时发现,结果并不是我们想要 ...

  6. postman 测试Excel文件导入导出功能

    1.导入的测试方法 选择form-data,key值填写方法对应的参数,选择File,Value处上传文件即可. 2. 导出的测试方法 在导出文件的时候,响应结果是乱码,然后在测试的时候选择下载,下载 ...

  7. coding git push 403 时

    直接修改 项目目录下的 .git/config   url url = https://coding用户名:coding密码@git.coding.net/coding账号/coding项目名称.gi ...

  8. Xcode7.2真机测试问题"The account 'appleID ' has no team with ID ‘’

     在Xcode7(测试版)提出免费真机测试的时候,我立刻在网上搜寻测试步骤,很简单,按照步骤走就可以. 但在7.2以后,突然我的iPhone不能真调了!提示"The account 'app ...

  9. IIS指定站点网卡IP进行网络部署

    服务器一般有多块网卡,有时候每块网卡会绑定不同的公网ip,也就是一个机器可以有多个公网ip,那IIS部署时可以选择此项目使用那个IP部署.(当然,一块网卡也可以绑定多个IP) 编辑绑定-编辑-IP地址 ...

  10. hbase 操作

    视频随笔视频地址:hbase教程 1.与传统关系型数据库的区别 hbase 传统分布式   单机列动态增减   建表时候指定只有字符串一种数据类型   数值,字符空值不被存储   存储不支持SQL 查 ...