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]

参考链接:

MDN Array

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 方法的更多相关文章

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

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

  2. ES5的 forEach, map 方法的实现

    如果浏览器不支持forEach,map方法, 要我们自己封装一个, 该怎么操作呢? 1. forEach Array.prototype.forEach = function(fn) { if (th ...

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

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

  4. js数组方法forEach,map,filter,every,some实现

    Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "fun ...

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

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

  6. forEach, map, filter方法区别

    听说for循环已经成了菜鸟标配...? 瑟瑟发抖 赶紧找来资料补一补 1, forEach循环,循环数组中每一个元素并采取操作, 没有返回值, 可以不用知道数组长度 2, map函数,遍历数组每个元素 ...

  7. 数组那些事(slice,splice,forEach,map,filter等等)

    周五,再过会要下班了,刚才把<javascript高级程序设计>数组这块又看了下,加深下记忆.今天来继续练练笔,嘿嘿!(写下自己印象不深的东西) 一.数组的定义(数组定义分为两种) 方法一 ...

  8. JS中some(),every(),forEach(),map(),filter()区别

    JS在1.6中为Array新增了几个方法map(),filter(),some(),every(),forEach(),也就是一共有这么多方法了. 刚开始接触这些倒也记得不是很清楚,在此纪录一下以加深 ...

  9. 处理数组的forEach map filter的兼容性

    处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback ...

随机推荐

  1. 一条SQL查出当月的每一天

    from master..spt_values ),,),'2013-02-03')+'-01' as datetime)) 结果: 返回带有年月日的日期 ),),) AS datetime) fro ...

  2. BizTalk发布WS-Security的web services

    最近做个项目,biztalk跟OTM(Oracle Transportation Management)系统做对接,双方通过web services通讯,这部分是BizTalk发布WS-Securit ...

  3. PHP基础知识2

    1.运算符 1.运算符简单来说就是用来连接各个常量.变量以及函数和其他表达式参与运算的符号! 2.运算符的优先级 2.流程控制 1.流程控制,就是指程序执行的"路线",一般是用相关 ...

  4. c++指针与引用问题

    本来是回答问题的,到这里做个笔记 *&L是指针的引用,实参是个指针.所以L是实参指针的别名,对别名L的修改,等于对实参的修改.*L是传值,你无法改变传过来的实参指针变量的值程序代码: #inc ...

  5. 如何通过JavaScript构建Asp.net服务端控件

    摘要 虽然ASP.NET的服务器控件一直被大家所诟病,但是用户控件(ACSX)在某些场景下还是非常有用的. 在一些极特珠的情况下,我们会使用JavaScript动态的构建页面中的控件,但假设遇到了我要 ...

  6. SQL Server选项综述

    I. 基本概念 SQL Server中的选项根据其作用范围分为如下几类: 实例选项 —— 在数据库实例范围内有效,通过 sp_configure 存储过程进行配置. 数据库选项 —— 在数据库范围内有 ...

  7. 常用PHP函数类目录

    说明:用来记录我在开发过程中,经常用到的一些常用函数或者常用类. 常用函数 PHP常用函数 (1) 常用类 PHP表单数据校验类

  8. 使用U盘安装win7系统

    --------------------------------------------------- 步骤1:制作可启动U盘(如果已经有可启动U盘则直接跳到步骤2) 1.下载系统镜像,请百度搜索“w ...

  9. Erlang 参考资料

    Erlang 官方文档 Distributed Erlang Erlang 教程中文版 %设定模块名 -module(tut17). %导出相关函数 -export([start_ping/1, st ...

  10. GHOST(幽灵)重大漏洞

    cd /usr/local/srcwget https://webshare.uchicago.edu/orgs/ITServices/itsec/Downloads/GHOST.cgcc GHOST ...