JavaScript 1.6 引入了几个新的Array 方法,具体的介绍见:New in JavaScript 1.6 。这些方法已经被写进了ECMA262 V5。现代浏览器(IE9/Firefox/Safari/Chrome/Opera)都已经支持,但IE6/7/8不支持。jquery的工具方法中提供了类似的功能。


1、Array.forEach()和jquery的$().each()。在数组中的每个项上运行一个函数。类似java5 增强的for循环

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

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

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

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

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

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

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

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

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

最后给出 IE6/7/8的解决方案,让这些浏览器完美支持JS1.6的Array新方法。

01 -function(){
02      
03 function applyIf(o, c) {
04     if(o) {
05         for(var in c) {
06             if(o[p]===undefined) {
07                 o[p] = c[p];
08             }
09         }
10     }
11     return o;
12 }
13 applyIf(Array.prototype, {
14     indexOf : function(obj, idx) {
15         var from = idx == null ? 0 : (idx < 0 ? Math.max(0, arr.length + idx) : idx);
16         for(var i = from, l = this.length; i < l; i++) {
17             if(i in this && this[i] === obj) {
18                 return i;
19             }
20         }
21         return -1;
22     },
23     lastIndexOf : function(obj, idx) {
24         var len = this.length, from = idx == null ? len - 1 : idx;
25         if(from < 0) {
26             from = Math.max(0, len + from);
27         }
28         for(var i = from; i >= 0; i--) {
29             if (i in this && this[i] === obj) {
30                 return i;
31             }
32         }
33         return -1;
34     },
35     every : function(fn, thisObj) {
36         var l = this.length;
37         for(var i = 0; i < l; i++) {
38             if(i in this && !fn.call(thisObj, this[i], i, this)) {
39                 return false;
40             }
41         }
42         return true;
43     },
44     some : function(fn, thisObj) {
45         var l = this.length;
46         for(var i = 0; i < l; i++) {
47             if(i in this && fn.call(thisObj, this[i], i, this)) {
48                 return true;
49             }
50         }
51         return false;
52     },
53     filter : function(fn, thisObj) {
54         var l = this.length, res = [], resLength = 0;
55         for(var i = 0; i < l; i++) {
56             if(i in this) {
57                 var val = this[i];
58                 if(fn.call(thisObj, val, i, this)) {
59                     res[resLength++] = val;
60                 }
61             }
62         }
63         return res;
64     },
65     map : function(fn, thisObj) {
66         var l = this.length, res = [];
67         for(var i = 0; i < l; i++) {
68             if(i in this) {
69                 res[i] = fn.call(thisObj, this[i], i, this);
70             }
71         }
72         return res;
73     },
74     forEach : function(fn, thisObj) {
75         var l = this.length;
76         for(var i = 0; i < l; i++) {
77             if(i in this) {
78                 fn.call(thisObj, this[i], i, this);
79             }
80         }
81     }
82 });
83 }();

本文转载:CSDN博客