1, $.map (array,function)
Parsing: Invokes a callback function for each element in the wrapper set and collects the return value into an instance of the JQuery object, which in the callback function points to the current DOM element in each iteration.
2. $.each (array,function) parsing: Each element in an array of arrays calls the function function, but there is no return value (so the effect is the same regardless of whether you add a return to the callback). The $.each () function can be used to iterate over any collection, whether it is a name/value object (JavaScript object) or an array. (1) Traversal array (with attachment parameters) $.each (array, function (P1, p2) {this; The This here points to the current element of the array in each traversal P1; P2; Access additional parameters}, [' Parameter 1 ', ' parameter 2 ']);
(2) Traversal object (no additional parameters) $.each (object, function (name, value) {this; This refers to the value of the current property name; The name of the current property of object value; Value represents the current property of the object});
The difference between the two: the map () method is mainly used to traverse the array of operations and objects, each () is used primarily to traverse the jquery object. Each () returns the original array, and does not create a new array. The map () method returns a new array. If you use map without the need, it can be a waste of memory. Test example: When iterating through an array, each has no return value, so it does not change its value with no return, and map does not affect the original array because it creates a new array. When iterating through the object, each time it changes the value of the parameter (no matter if the return has a wood add), the original object is changed, and the map traversal changes the parameter value (must add return, otherwise return []), the new object and the original object is changed.
varObj1 = [10,20]; $.each (Obj1,function(I,param) {returnparam+1;
}); [Ten]//obj1:[10,20]varOBJ2 = [10,20]; $.each (Obj2,function(I,param) {return[Param+1];
}); [Ten]//obj2:[10,20]varOBJ3 = [10,20]; $.each (OBJ3,function(I,param) {return[[Param+1]];
}); [Ten]//obj3:[10,20]varOBJ4 = [10,20]; $.map (OBJ4,function(param) {returnparam+1;
}); [One, a]//obj4:[10,20]varObj5 = [10,20]; $.map (Obj5,function(param) {return[Param+1];
}); [One, a]//obj5:[10,20]varOBJ6 = [10,20]; $.map (Obj6,function(param) {return[[Param+1]];
}); [[11],[21]]//obj6:[10,20]varOBJ7 = {1:{id: ' 001 '}, 2:{id: ' 002 '}}; $.each (OBJ7,function(I,param) {return(param.id + = 1);
});
{1:{id: ' 0011 '},2:{id: ' 0021 '}; Obj7:{1:{id: ' 0011 '},2:{id: ' 0021 '}varOBJ7 = {1:{id: ' 001 '}, 2:{id: ' 002 '}}; $.each (OBJ7,function(I,param)
{(param.id + = 1);});
{1:{id: ' 0011 '},2:{id: ' 0021 '}; Obj7:{1:{id: ' 0011 '},2:{id: ' 0021 '}varObj8 = {1:{id: ' 001 '}, 2:{id: ' 002 '}}; $.map (Obj8,function(param) {returnParam.id + = 1;
}); ["0011", "0021"]//obj8:{1:{id: ' 0011 '},2:{id: ' 0021 '}}varObj8 = {1:{id: ' 001 '}, 2:{id: ' 002 '}}; $.map (Obj8,function(param)
{param.id + = 1;}); []//obj8 value: {1:{id: ' 0011 '},2:{id: ' 0021 '}}varObj9 = {1:{id: ' 001 '}, 2:{id: ' 002 '}}; $.each (Obj9,function(I,param) {return[[Param.id + 1]];
}); {1:{id: ' 0011 '},2:{id: ' 0021 '}}//obj9:{1:{id: ' 0011 '},2:{id: ' 0021 '}}varObj10 = {1:{id: ' 001 '}, 2:{id: ' 002 '}}; $.map (Obj10,function(param) {return[[Param.id + 1]];
}); [["0011"],["0021"]]//obj10 value: {1:{id: ' 0011 '},2:{id: ' 0021 '}}