본문 바로가기

Web/Javascript

JavaScript - MAP

map() 메소드는 파라미터로 전달 된 함수를 통하여 배열 내의 각 요소를 처리해서 그 결과로 새로운 배열을 생성합니다.




arr.map(callback, [thisArg])


callback 새로운 배열의 요소를 생성하는 함수로서, 다음 세가지 인수를 가집니다.

      currentValue 현재 처리되고 있는 요소

      index 현재 처리되고 있는 요소의 index 값

      array 메소드가 불려진 배열

thisArg (선택항목) callback 함수 내부에서 사용 할 this 값을 설정


var numbers = [1, 2, 3, 4, 5];
 
var processed = numbers.map(function(num){
    return num*num;
});
/* ES6 Syntax */
let numbers = [1, 2, 3, 4, 5];
 
let result = numbers.map((num) => {
    return num*num;
});

결과: [1, 4, 9, 16, 25]



arrow function ( ... ) => { ... }


http://es6console.com/



출처 : http://slides.com/minjunkim-1/

'Web > Javascript' 카테고리의 다른 글

JavaScript - FILTER  (0) 2017.03.02
JavaScript - SORT  (0) 2017.03.02
2016년에 자바스크립트를 배우는 기분  (0) 2017.02.22
JS 라이브러리  (0) 2017.02.19
jQuery.browser  (0) 2017.02.15