ES5 的 Array

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]; // 1

// 只能进行浅复制
var arr = [{a: 1}, 2];
var arrCopy = Array.from(arr);
arrCopy[0].a; // 1
arrCopy[0].a = 2;
arr[0].a; // 2

// 转换 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/

posted @ 2016-07-05 16:20  zhengming  阅读(236)  评论(0编辑  收藏  举报