Introduction to the new features of JavaScript1.6 array and several JQuery tools and methods _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the new features of JavaScript1.6 array and several JQuery tools and methods. If you need them, please refer to them, I hope to help you introduce several New Array methods in JavaScript 1.6. For details, see New in JavaScript 1.6. These methods have been written into ECMA262 V5. Modern browsers (IE9/Firefox/Safari/Chrome/Opera) are supported, but IE6/7/8 is not. Jquery provides similar functions in tool methods.


1. Array. forEach () and $ (). each () of jquery ().Run a function on each item in the array. Similar to the Java 5 enhanced for loop.

The Code is as follows:


Var ary = [2, 4, 6, 8];

// Js1.6 Array. forEach Method
Ary. forEach (function (I) {alert (I );});

// Jquery statement
$ (Ary). each (function () {alert (this );});
// You can also write it like this
$ (Ary). each (function (index, item) {alert (item) ;}); // index is the index of an element. item is the element.


2. $. grep () of Array. filter () and jquery ().Run a function on each item in the array and return the item that returns the true value of the function as an array. Simply put, a condition is used to filter out non-conforming array elements, and the remaining elements that meet the condition are combined into a new array to return.

The Code is as follows:


Var ary = [2, 4, 6, 8];

// Js1.6 Array. filter () method
Var otherAry1 = ary. filter (function (item) {return item> 4 ;});
Alert (otherAry1); // outputs 6, 8

// Jquery method (note the difference with $. each)
// In this function, the first parameter is the array element, and the second parameter is the array element index.
// While the $ (). each method is the opposite. The author should unify it.
Var otherAry2 = $. grep (ary, function (item, index ){
Return item> 4;
});
Alert (otherAry2); // outputs 6, 8


3. $. map () of Array. map () and jquery ().Run a function on each item in the array and return all results as an array. This method is very powerful, especially when it is applied to DOM arrays (used in the abcc project to generate query strings for each query module DOM ). Simply put, the result of each array element operation is used as a new array element (which is still very difficult ).

The Code is as follows:


Var ary = [2, 4, 6, 8];

// Js1.6 Array. map () method
Var newAry1 = ary. map (function (item) {return item + 1 ;}); // Add 1 to each element
Alert (newAry1); // outputs 3, 5, 7, 9

// Jquery Writing Method
Var newAry2 = $. map (ary, function (item, index) {return item + 1 ;});
Alert (newAry2); // outputs 3, 5, 7, 9


4. Array. every () method.Check whether all elements in the array meet a certain condition. If one element does not meet the condition, false is returned. Otherwise, true is returned.

The Code is 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.Check whether the element in the array meets a certain condition. If one element meets the condition, true is returned. Otherwise, false is returned.

The Code is 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 solution for IE6/7/8 is provided, so that these browsers can perfectly support the new Array method of JS1.6.

The Code is 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 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 );
}
}
}
});
}();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.