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 ...
随机推荐
- delphi locate多字段查询
简单格式: IF MSQ_NewBillQuantity.Locate('FStockID;FMarchID', VarArrayOf([FStockID, FMarchID]), []) = Fal ...
- Processing 电子罗盘校准(以 MPU9250为例)
使用Processing 软件, 通过 arduino 输入 电子罗盘的数据,通过PC端进行校准,程序如下: import processing.serial.*; Serial myPort; Ar ...
- 记录Tomcat7.x热部署配置过程
我自己的开发版本是tomcat7.0.43+myeclipse14 原版在:http://blog.csdn.NET/chen_zw/article/details/8867779 热部署是指在你对项 ...
- 彻底搞定char/wchar_t/unicode
彻底搞定char/wchar_t!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (2013-07-17 10:18:28) 转载▼ 从char/wchar_t到TCHAR(1) ...
- Mac 配置Charles抓https的包
安装Charles 这个简单,略过... 打开Charles,在Menu选择Help > Install Charles CA SSL Certificate Keychain Access(钥 ...
- <2016-2-2 总结>
真正的伟人直率真诚,真正的贤人虚怀若谷,真正的强者温文尔雅,有足够的幽默感,尽可能的庄重而不盛气凌人! 真正的伟人直率真诚,真正的贤人虚怀若谷,真正的强者温文尔雅,有足够的幽默感,尽可能的庄重而不盛气 ...
- day10-rabbitmq
安装python rabbitMQ module pip instal pika 发布者: #!/usr/bin/env python #coding:utf8 import pika connect ...
- 开源的运维机器人hubot原理
- LeedCde 题解目录
1. Longest Palindromic Substring ( 最长回文子串 ) 2. Median of Two Sorted Arrays (两个排序数组的中位数) 3. Sqrt(x) 4 ...
- 给 VS 2010 选一个好用的代码行数统计器(转)
给 VS 2010 选一个好用的代码行数统计器 分类: Tricks2011-02-25 05:40 3891人阅读 评论(0) 收藏 举报 2010c 推荐一个VS插件,支持2005/2008/20 ...