본문 바로가기
카테고리 없음

[jQuery] map vs each

by palbokdev 2023. 9. 16.
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]

each : 기존 배열 반환

map : 새 배열 반환 (요소를 추가하거나 제거 가능)

 

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