JavaScript 1.6 introduces several new array methods, which are described in detail: New in JavaScript 1.6. These methods have been written into the ECMA262 V5. Modern browsers (Ie9/firefox/safari/chrome/opera) have been supported, but IE6/7/8 are not supported. A similar functionality is provided in the jquery tool approach.
1, Array.foreach () and jquery $ (). each (). run a function on each item in the array. A For loop similar to the Java5 enhancement.
Copy Code code as follows:
var ary = [2,4,6,8];
js1.6 Array.foreach method
Ary.foreach (function (i) {alert (i);});
The writing of jquery
$ (ary). each (function () {alert (this);});
You can also write this
$ (ary). each (function (index,item) {alert (item);}); /index is the index of the element, and item is the element
2, Array.filter () and jquery $.grep (). Runs a function on each item in the array and returns an entry that returns the value of the function as an array. Simply put, a condition is used to filter out the inconsistent array elements, and the remaining qualifying element groups are synthesized to return the new array.
Copy Code code as follows:
var ary = [2,4,6,8];
js1.6 Array.filter () method
var otherAry1 = ary.filter (function (item) {return item>4;});
alert (otherAry1);//Output 6,8
jquery notation (note and $.each difference)
The first argument in the function is the array element itself, and the second argument is the array element index
and $ (). Each method is just the opposite, the author should be unified.
var otherAry2 = $.grep (ary,function (item,index) {
Return item>4;
});
alert (otherAry2);//Output 6,8
3, Array.map () and jquery $.map (). Runs a function on each item in the array and returns the entire result as an array. This method is very powerful, especially when acting on a DOM array (used on a ABCC project, generating a query string for each query module DOM). Simply put, the result of each array element operation is a new array element (or a mouthful).
Copy Code code as follows:
var ary = [2,4,6,8];
js1.6 Array.map () method
var newAry1 = Ary.map (function (item) {return item+1;}); /each element plus 1
alert (newAry1);//Output 3,5,7,9
jquery notation
var newAry2 = $.map (ary,function (item,index) {return item+1;});
alert (newAry2);//Output 3,5,7,9
4, Array.every () method. Checks whether the array element conforms to a condition and returns true if one does not conform to return false
Copy Code code as follows:
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 () method. Returns False if the element in the array conforms to a condition, as long as one of the matches returns true
Copy Code code as follows:
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
Finally, the IE6/7/8 solution is given to enable these browsers to support the JS1.6 array new method perfectly.
Copy Code code as follows:
-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 here &&!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 here && 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);
}
}
}
});
}();