The use of each function in jquery looks almost the same as the map function, but there is a difference.
One important difference is that each returns the original array, and no new array is created. The map method returns a new array. If you use a map if it is not necessary, you can create a waste of memory.
e.g.
var items = [1,2,3,4]; $.each (items, function () {
alert (' it ' + this);
});
var newitems = $.map (items, function (i) {return
i + 1;
});
NewItems is [2,3,4,5]
When you use each, you change the original items array, and when you use map, do not change items, just create a new array.
e.g.
var items = [0,1,2,3,4,5,6,7,8,9]; var itemslessthanequalfive = $.map (items, function (i) {
//Removes all items > 5
if (i > 5)
ret Urn null;
return i;
});
Itemslessthanequalfive = [0,1,2,3,4,5]
This is also true when an array is required for deletion, so the error of using each or map for deletion is quite serious.