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循環。
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的解決方案,讓這些瀏覽器完美支持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);
}
}
}
});
}();