ES5 的 forEach, map, filter, some, every 方法
1: Array.isArray
判断是否为数组
Array.isArray([1, 2, 3]); // true
Array.isArray({foo: 123}); // false // Polyfill
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
2: forEach
array.forEach(callback,[ thisObject])
// 遍历数组里面的所有数字
// item 是值, i 是序号, array 是整个数组
[1, 2 ,3, 4].forEach(function(item, i, array){
/*
0 1 [1, 2, 3, 4]
1 2 [1, 2, 3, 4]
2 3 [1, 2, 3, 4]
3 4 [1, 2, 3, 4]
*/
console.log(i, item, array);
});
第2个参数为上下文:
var obj = {
arr: ['a', 'b', 'c', 'd'],
check: function(item, i, array){
console.log(this.arr[i]);
}
}
// 输出 a, b, c, d
obj.arr.forEach(obj.check, obj);
3: map
array.map(callback,[ thisObject]);
遍历数组,返回 return 的值组成新的数组
var arr = [1, 2, 3, 4]; // arr 数组不变
var newArr = arr.map(function (item, i, array) {
return item * item;
}); // newArr 为 [1, 4, 9, 16]
4: filter
返回值只要是弱等于== true/false, 就会放到新的数组里
// 找到是偶数的数字
var arr = [1,2,3,4,5]; // arr 数组不变
var newArr = arr.filter(function(item, i){
if(item % 2 == 0){
return true;
}else{
return false;
}
}); // newArr 为 [2, 4]
var data = [0, false, true, 1, 2, 3];
var newArr = data.filter(function (item, i, array) {
return item;
}); // newArr 为 [true, 1, 2, 3]
5: some
返回Boolean值, true 或 false
当callback 遍历每一个值的时候有一个值返回 true, 结束遍历, res就为true,
若所有的都 返回false, res才为false
// 下面的代码当遍历到 3时, 就不会继续遍历到 4了
var arr = [1, 2, 3, 4]; // arr 数组不变
var res = arr.some(function(item, i, array){
/*
1 0 [1, 2, 3, 4]
2 1 [1, 2, 3, 4]
3 2 [1, 2, 3, 4]
*/
console.log(item, i, array);
return item > 2;
});
// res 的值为 true;
6: every
返回Boolean值, true 或 false
当callback 遍历每一个值的时候若有一个返回 false, 结束遍历, res为 false,
若所有的 返回true, res才为true
var arr = [1, 2, 3, 4]; // arr 数组不变
var res = arr.every(function(item, i, array){
/*
1 0 [1, 2, 3, 4]
*/
console.log(item, i, array);
return item > 2;
});
7: reduce
8: reduceRight
9: Array.from (Android No support)
The Array.from() method creates a new Array instance from an array-like or iterable object.
// 简单复制
var arr = [1, 2];
var arrCopy = Array.from(arr);
arrCopy[0] = 2;
arr[0]; // // 只能进行浅复制
var arr = [{a: 1}, 2];
var arrCopy = Array.from(arr);
arrCopy[0].a; //
arrCopy[0].a = 2;
arr[0].a; // // 转换 arguments 为数组
var f = function(){
// 或者 [].slice.apply(arguments);
var arg = Array.from(arguments);
console.log( Array.isArray(arg) );
}
f(); // true
10: Array.of ( Android No support )
The Array.of() method creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
The difference between Array.of() and the Array constructor is in the handling of integer arguments: Array.of(7) creates an array with a single element, 7, whereas Array(7) creates an empty array with a length property of 7 (Note: this implies an array of 7 empty slots, not slots with actual undefined values).
Array.of(7); // [7]
Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ]
Array(1, 2, 3); // [1, 2, 3]
参考链接:
http://www.zhangxinxu.com/wordpress/2013/04/es5%E6%96%B0%E5%A2%9E%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95/
ES5 的 forEach, map, filter, some, every 方法的更多相关文章
- 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight
做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...
- ES5的 forEach, map 方法的实现
如果浏览器不支持forEach,map方法, 要我们自己封装一个, 该怎么操作呢? 1. forEach Array.prototype.forEach = function(fn) { if (th ...
- ES6新增的常用数组方法(forEach,map,filter,every,some)
ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...
- js数组方法forEach,map,filter,every,some实现
Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "fun ...
- ES6 数组方法 forEach map filter find every some reduce
1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(pa ...
- forEach, map, filter方法区别
听说for循环已经成了菜鸟标配...? 瑟瑟发抖 赶紧找来资料补一补 1, forEach循环,循环数组中每一个元素并采取操作, 没有返回值, 可以不用知道数组长度 2, map函数,遍历数组每个元素 ...
- 数组那些事(slice,splice,forEach,map,filter等等)
周五,再过会要下班了,刚才把<javascript高级程序设计>数组这块又看了下,加深下记忆.今天来继续练练笔,嘿嘿!(写下自己印象不深的东西) 一.数组的定义(数组定义分为两种) 方法一 ...
- JS中some(),every(),forEach(),map(),filter()区别
JS在1.6中为Array新增了几个方法map(),filter(),some(),every(),forEach(),也就是一共有这么多方法了. 刚开始接触这些倒也记得不是很清楚,在此纪录一下以加深 ...
- 处理数组的forEach map filter的兼容性
处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback ...
随机推荐
- Percona Toolkit 使用
安装 percona-toolkit perl Makefile.PL make make test make install 默认安装到 /usr/local/bin 目录下 可能需要 DBI-1. ...
- policy
template <class Apolicy> class Host { Apolicy direct_policy_use; Apolicy <SomeInternalT ...
- form表单的enter自动提交
当form中只有一个文本框时并且获得焦点 按enter时,就会自动提交表单.阻止自动提交 可以添加一个隐藏的input框 <input type="text" style=& ...
- Python基础篇【第3篇】: Python异常处理、反射、动态导入、利用反射的web框架
异常处理 什么是异常? 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当P ...
- Hadoop_配置_linux下编译eclipse插件
使用的hadoop版本为hadoop-1.2.1(对应的含源码的安装包为hadoop-1.2.1.tar.gz) 将hadoop和eclipse都解压在home中的用户目录下 /home/chen/h ...
- 45个android实例源码
分享45个android实例源码,很好很强大http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码http://www.apkbus.com ...
- 在CentOS或RHEL防火墙上开启端口
转载自:https://linux.cn/article-4243-1.html 如果希望在服务器上提供服务,诸如CentOS或RHEL的企业级Linux发行版包含内置的强大防火墙,它们默认的防火墙规 ...
- ubuntu12.04 gitlab搭建
最近在尝试内部搭建gitlab,wiki这些工具...我使用的官网的gitlab-ce包一键安装,自己搭建的ubuntu12.04 server服务器. 分配253地址,放在办公室的小角落. 配置过程 ...
- PHP中类自动加载的方式
最近在学习composer,发现从接触PHP到现在已经遇到了三种关于PHP中类的自动加载方式,这其中包括PHP自带的类的自动加载方式.PHP的第三方的依赖管理工具composer的加载方式以及PHP的 ...
- Mybatis 新增修改一条SQL
如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE:如果不会导致唯一值 ...