---
title: "JavaScript1.6数组新特性和JQuery的几个工具方法"
author: "weistar"
date: "2014-10-24T15:02:19Z"
tags: []
categories: ["编程语言"]
slug: "javascript1-6-jquery"
draft: true
lastmod: "2014-10-24T15:02:19Z"
id: 2147574
url: "https://weistar.iteye.com/blog/2147574"
---

JavaScript 1.6 引入了几个新的Array 方法，具体的介绍见：[New in JavaScript 1.6](https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.6#Array_extras) 。这些方法已经被写进了ECMA262 V5。现代浏览器（IE9/Firefox/Safari/Chrome/Opera）都已经支持，但IE6/7/8不支持。jquery的工具方法中提供了类似的功能。

 

**1、Array.forEach()和jquery的$().each()。**在数组中的每个项上运行一个函数。类似java5 [增强的for循环](http://www.ibm.com/developerworks/cn/java/j-forin.html)。

```
var ary = [2,4,6,8];
 
// js1.6 Array.forEach方法
ary.forEach(function(i){alert(i);});
 
// jquery的写法
$(ary).each(function(){alert(this);});
//还可以写成这样
$(ary).each(function(index,item){alert(item);});//index是元素的索引，item是该元素
```

 

**2、Array.filter()和jquery的$.grep()。**在数组中的每个项上运行一个函数，并将函数返回真值的项作为数组返回。简单的说就是用一个条件过滤掉不符合的数组元素，剩下的符合条件的元素组合成新的数组返回。

```
var ary = [2,4,6,8];
 
// js1.6 Array.filter()方法
var otherAry1 = ary.filter(function(item){return item>4;});
alert(otherAry1);//输出6，8
 
// jquery写法(注意和$.each的区别)
// 此处函数中第一个参数是数组元素自身，第二个参数是数组元素索引
// 而$().each方法刚好相反，作者应该统一下。
var otherAry2 = $.grep(ary,function(item,index){
     return item>4;
});
alert(otherAry2);//输出6，8
```

 

**3、Array.map()和jquery的$.map()。**在数组中的每个项上运行一个函数，并将全部结果作为数组返回。这个方法非常强大，尤其是作用于DOM数组时（在abcc项目上用过，对每个查询模块DOM生成查询字符串）。简单说就是把每个数组元素运算的结果作为新数组元素（还是很拗口）。

```
var ary = [2,4,6,8];
 
// js1.6 Array.map()方法
var newAry1 = ary.map(function(item){return item+1;});//每个元素加1
alert(newAry1);//输出3，5，7，9
 
// jquery写法
var newAry2 = $.map(ary,function(item,index){return item+1;});
alert(newAry2);//输出3，5，7，9
```

 

**4、Array.every()方法。**检查数组元素是否都符合某个条件，只要有一个不符合返回false，否则返回true

```
var ary = [2,4,6,8,10];
 
alert(ary.every(function(item){return item>1}));//true
alert(ary.every(function(item){return item>2}));//false
```

 

**5、Array.some()方法。**检查数组中元素是否符合某个条件，只要有一个符合返回true，否则返回false

```
var ary = [2,4,,6,8,10];
 
alert(ary.some(function(item){return item>9;}));//true
alert(ary.some(function(item){return item>10;}));//false
```

 

最后给出 [IE6/7/8的解决方案](http://files.cnblogs.com/snandy/IE678_Array.js)，让这些浏览器完美支持JS1.6的Array新方法。

```
function(){
     
function applyIf(o, c) {
    if(o) {
        for(var p in c) {
            if(o[p]===undefined) {
                o[p] = c[p];
            }
        }
    }
    return o;
}
applyIf(Array.prototype, {
    indexOf : function(obj, idx) {
        var from = idx == null ? 0 : (idx < 0 ? Math.max(0, arr.length + idx) : idx);
        for(var i = from, l = this.length; i < l; i++) {
            if(i in this && this[i] === obj) {
                return i;
            }
        }
        return -1;
    },
    lastIndexOf : function(obj, idx) {
        var len = this.length, from = idx == null ? len - 1 : idx;
        if(from < 0) {
            from = Math.max(0, len + from);
        }
        for(var i = from; i >= 0; i--) {
            if (i in this && this[i] === obj) {
                return i;
            }
        }
        return -1;
    },
    every : function(fn, thisObj) {
        var l = this.length;
        for(var i = 0; i < l; i++) {
            if(i in this && !fn.call(thisObj, this[i], i, this)) {
                return false;
            }
        }
        return true;
    },
    some : function(fn, thisObj) {
        var l = this.length;
        for(var i = 0; i < l; i++) {
            if(i in this && fn.call(thisObj, this[i], i, this)) {
                return true;
            }
        }
        return false;
    },
    filter : function(fn, thisObj) {
        var l = this.length, res = [], resLength = 0;
        for(var i = 0; i < l; i++) {
            if(i in this) {
                var val = this[i];
                if(fn.call(thisObj, val, i, this)) {
                    res[resLength++] = val;
                }
            }
        }
        return res;
    },
    map : function(fn, thisObj) {
        var l = this.length, res = [];
        for(var i = 0; i < l; i++) {
            if(i in this) {
                res[i] = fn.call(thisObj, this[i], i, this);
            }
        }
        return res;
    },
    forEach : function(fn, thisObj) {
        var l = this.length;
        for(var i = 0; i < l; i++) {
            if(i in this) {
                fn.call(thisObj, this[i], i, this);
            }
        }
    }
});
}();
```