js map、filter、forEach
1、map方法
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>map方法</title>
</head> <body>
<script type="text/javascript">
var arr = [6, 5, 4];
var arrNew = [];
arrNew = arr.map(function(value, index, arr) {
console.log('当前值:' + value);
console.log('当前值对应的索引:' + index);
console.log('原数组:' + arr);
return value * value
});
//map返回新数组
console.log(arrNew);
</script>
</body> </html>
控制台输出为:

2、filter方法
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>filter方法</title>
</head> <body>
<script type="text/javascript">
var arr = [6, 5, 4];
var arrNew = [];
arrNew = arr.filter(function(value, index, arr) {
console.log('当前值:' + value);
console.log('当前值对应的索引:' + index);
console.log('原数组:' + arr);
return value % 2 == 0;
});
//map返回新数组
console.log(arrNew);
</script>
</body> </html>
控制台输出为:

3、forEach方法
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>forEach方法</title>
</head> <body>
<script type="text/javascript">
var arr = [6, 5, 4];
var total = 0;
arr.forEach(function(value, index, arr) {
console.log('当前值:' + value);
console.log('当前值对应的索引:' + index);
console.log('原数组:' + arr);
total += value
});
console.log(total);
</script>
</body> </html>
控制台输出:

总结:以上函数的回调函数的形参是一样的。
注意:jQuery中的each的参数是:
$(selector).each(function(index,element))
是不一样的顺序。
js map、filter、forEach的更多相关文章
- (转载)js数组中的find、filter、forEach、map四个方法的详解和应用实例
数组中的find.filter.forEach.map四个语法很相近,为了方便记忆,真正的掌握它们的用法,所以就把它们总结在一起喽. find():返回通过测试的数组的第一个元素的值 在第一次调用 c ...
- JavaScript数组方法的兼容性写法 汇总:indexOf()、forEach()、map()、filter()、some()、every()
ECMA Script5中数组方法如indexOf().forEach().map().filter().some()并不支持IE6-8,但是国内依然有一大部分用户使用IE6-8,而以上数组方法又确实 ...
- JavaScript高级编程——Array数组迭代(every()、filter()、foreach()、map()、some(),归并(reduce() 和reduceRight() ))
JavaScript高级编程——Array数组迭代(every().filter().foreach().map().some(),归并(reduce() 和reduceRight() )) < ...
- react+redux教程(三)reduce()、filter()、map()、some()、every()、...展开属性
reduce().filter().map().some().every()....展开属性 这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...
- ES5新增数组方法every()、some()、filter()、map()
JavaScript ES5标准中新增了一些Array方法,如every().some().filter().map().它们的出现使我们能够更加便利地操作数组,但对IE9以下浏览器的兼容性比较差.下 ...
- reduce()、filter()、map()、some()、every()、...展开属性
reduce().filter().map().some().every()....展开属性 这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...
- 辅助函数和高阶函数 map、filter、reduce
辅助函数和高阶函数 map.filter.reduce: 一.辅助函数:(1-1)响应式函数 (数组更新检测): push() pop() shift() unshift() ...
- Python基础三. 函数、lambda、filter、map、reduce
一.概述 函数, 就是用一些语句组织起来实现一组特定的功能, 用来重复调用. 函数的作用及意义:最大化的重用代码和最小化的代码冗余以及对流程的分解. Python中有哪些函数: 内建的函数 第三方模块 ...
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
随机推荐
- hdu 5747(数学,贪心)
Aaronson Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- (转)Docker 基础 : Dockerfile
全文来自 Docker 基础 : Dockerfile Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 快速创建自定义的镜像.我们会先介绍 Dockerfile 的 ...
- (24)python 无线破解
import pywifi import time wifi = pywifi.PyWiFi() # iface = wifi.interfaces()[0]#获取第一块网卡 #print(iface ...
- 康托展开&逆展开算法笔记
康托展开(有关全排列) 康托展开:已知一个排列,求这个排列在全排列中是第几个 康托展开逆运算:已知在全排列中排第几,求这个排列 定义: X=an(n-1)!+an-1(n-2)!+...+ai(i-1 ...
- linux根目录下文件夹概览详解
/ 根目录 /bin 存放必要的命令 /boot 存放内核以及启动所需的文件等 /dev 存放设备文件 /etc 存放系统的配置文件 /home 用户文件的主目录,用户数据存放在其主目录中 /lib ...
- (寒假GYM开黑)2018-2019 ACM-ICPC Brazil Subregional Programming Contest
layout: post title: 2018-2019 ACM-ICPC Brazil Subregional Programming Contest author: "luowenta ...
- NOIP2018提高组模拟题(四)
能量(energy) Description 有一块能量田,它的形状是 n*m的矩形,每一个格子上都有一个能量值 a[x][y] (可正可负).一块矩形田的能量定义为它的每个格子的能量值之和. ...
- [BZOJ 3144] 切糕
Link: BZOJ 3144 传送门 Solution: 发现要把点集分成不连通的两部分,最小割的模型还是很明显的 首先我们将原图转化为$R+1$层,从而将点权化为边权 关键还是在于建图是怎么保证$ ...
- Problem T: 零起点学算法15——交换变量
#include<stdio.h> int main() { int a,b,c; scanf("%d %d",&a,&b); c=a; a=b; b= ...
- Androids中数据库的使用SQLite
(一) 知识点: SQLite3支持的数据类型:NULL,INTEGER,REAL(浮点数字),TEXT(字符串文本),BLOB(二进制对象),虽然他支持的类型只有五种,但实际上sqlite3也接受v ...