Array.filter (function (Current,index,arr), thisvalue);
Function: Filter out an array that matches the criteria
Array.filter (Current,index,arr) {
Example: Var list=aqidata.filter (function (Item,index,array) {
return (ITEM[1]>=60);
Current: Required, the value of the currently-selected element
Index: optional, indexed value of the current element
Arr: Optional,
Thisvalue: Optional, the object is used as the execution callback, passed to the function, and used as the value of "this". If omitted, the value of this is undefined
}
Array.Sort (function (b) {
return a-B;
Array: return a[1]-b[1];
})
First, native JS ForEach () and map () traversal
Common:
1. All loops iterate through each item in the array.
Each execution of the anonymous function in 2.forEach () and map () supports 3 parameters: the current item in the array item, the index of the current item, and the original array input.
3. This in the anonymous function refers to window.
4. Only the array can be traversed.
1.forEach ()
no return value.
Arr[].foreach (function (Value,index,array) {
Do something
})
- Parameter: The current item in the value array, index of the current item, array original array;
- There are several items in the array, then the anonymous callback function passed in will need to be executed several times;
- Theoretically, this method has no return value, simply iterates through each item in the array, does not modify the original array, but can modify the original array by itself by the index of the array;
- var ary = [12,23,24,42,1];
- var res = Ary.foreach (function (item,index,input) {
- Input[index] = item*10;
- })
- Console.log (RES); //--> undefined;
- Console.log (ary); //--> changes the original array by the array index;
2.map ()
There is a return value, you can return it.
Arr[].map (function (Value,index,array) {
Do something
Return XXX
})
- Parameter: The current item in the value array, index of the current item, array original array;
- Difference: The return value is supported in the callback function of map, and what is returned is equivalent to what the item in the array is (it does not affect the original array, but it is equivalent to cloning a copy of the original array and changing the corresponding item in the array of the cloned copy);
- var ary = [12,23,24,42,1];
- var res = Ary.map (function (item,index,input) {
- return item*10;
- })
- Console.log (RES); //-->[120,230,240,420,10]; A copy of the original array was copied and modified
- Console.log (ary); //-->[12,23,24,42,1]; The original array has not changed
Compatible wording:
Either foreach or map is incompatible under IE6-8 (incompatible cases do not have these two methods on Array.prototype), then we need to encapsulate a compatible method, the code is as follows:
- /**
- * foreach Traversal array
- * @param callback [function] callback function;
- * @param context [object] context;
- */
- Array.prototype.myForEach = function Myforeach (callback,context) {
- Context = Context | | Window
- if (' ForEach ' in array.prototye) {
- This.foreach (Callback,context);
- return;
- }
- //ie6-8 Write your own callback function execution Logic
- For (var i = 0,len = this.length; i < len;i++) {
- Callback && Callback.call (context,this[i],i, this);
- }
- }
- /**
- * Map Traversal array
- * @param callback [function] callback function;
- * @param context [object] context;
- */
- Array.prototype.myMap = function MyMap (callback,context) {
- Context = Context | | Window
- if (' map ' in array.prototye) {
- return This.map (callback,context);
- }
- //ie6-8 Write your own callback function execution Logic
- var newary = [];
- For (var i = 0,len = this.length; i < len;i++) {
- if (typeof callback = = = ' function ') {
- var val = callback.call (context,this[i],i, this);
- Newary[newary.length] = val;
- }
- }
- return newary;
- }
Ii. jQuery $.each () and $.map () traversal
Common:
You can iterate through the array and iterate over the object.
1.$.each ()
no return value. The anonymous function inside the $.each () supports 2 parameters: The current item's index I, the current item in the array, v. If the object is traversed, K is the key and V is the value.
$.each (arr, function (index,value) {
Do something
})
- Parameters: Arr to iterate through the array, index the current item's indices, and the current item in the value array
- The 1th and 2nd parameters are exactly the opposite of the above two functions, and be careful not to remember the wrong one.
- $.each (["a","B","C"], function (i, v) {
- Alert (i + ":" + V);
- });
- $ ("span"). each (function (i, v) {
- Alert (i + ":" + V);
- });
- $.each ({name: "John", Lang: "JS"}, function (k, v) {
- Alert ( "Name:" + K + ", Value:" + V);
- });
2.$.map ()
There is a return value, you can return it. The anonymous function inside $.map () supports 2 parameters and $.each () in the opposite position: the current item in the array V, the index of the current item. If the object is traversed, K is the key and V is the value. If it is $ ("span"). Map () Form, parameter Order and $.each () $ ("span"). each ().
$.map (arr, function (value, index) {
Do something
Return XXX
})
- var arr=$.map ([0,1,2], function (v) {
- return v + 4;
- });
- Console.log (arr);
- $.map ({"name":"Jim","age": +},function (k, v) {
- Console.log (k +":" +v);
- });
Methods of arrays