코드/JS

jQuery each, map

Yeah-Panda 2014. 5. 30. 16:20




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.


For example:

var items = [1,2,3,4];

$.each(items, function() {
  alert('this is ' + this);
});

var newItems = $.map(items, function(i) {
  return i + 1;
});
// newItems is [2,3,4,5]

You can also use the map function to remove an item from an array. For example:

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) 
    return null;
  return i;
});
// itemsLessThanEqualFive = [0,1,2,3,4,5]



http://stackoverflow.com/questions/749084/jquery-map-vs-each

'코드 > JS' 카테고리의 다른 글

자바스크립트 배열 중복요소 추출  (0) 2015.04.20
Javascript 끝 문자열 제거  (0) 2015.04.08
팝업창에 데이터 전달하기.  (0) 2014.03.20
자바스크립트기초 - 프로토타입  (0) 2013.12.06
컨텍스트, 스코프  (0) 2013.12.05