2
Reply

What is difference between jquery .map() and .each()?

Manas Mohapatra

Manas Mohapatra

Aug 13, 2015
2k
0

    The each method is meant to be an immutable iterator, where as the map method can be used as an iterator, but is really meant to manipulate the supplied array and return a new array.Another important thing to note is that the each function returns the original array while the map function returns a new array. If you overuse the return value of the map function you can potentially waste a lot of memory.

    Munesh Sharma
    May 04, 2016
    0

    Both are jQuery functions which helps to loop over object list(json, array) in javascript.map() helps to loop over object list and also helps to return new array. each() helps only to loop over object list.Secondly you can break iteration in each() but it won't happen in map().var items = [1,2,3,4];$.each(items, function() {alert('this is ' + this); });var newItems = $.map(items, function(i) {return i + 1; });Output: [2,3,4,5]

    Manas Mohapatra
    August 13, 2015
    0