개발노트/Javascript

(JS) map() (2019/10/23)(보충필요!!)

superpil 2019. 10. 23. 17:17

개념


배열 내의 모든 요소  각각에 대해서 map()의 return값을 적용시켜 새로운 함수로 만든다.

foreach()처럼 배열의 length만큼 반복시켜 배열 값들에게 return값을 적용.

 

Array.map(callbackFunction(elementValue, index, array)

 

1. elementValue

배열 내에서 현재 값

 

2. index

배열 내에서 현재 인덱스 번호

 

3. array

배열 length만큼 배열을 출력한다.

 

 

사용방법


let mapping = [1, 2, 3, 4, 5];

let remap = mapping.map(function(element){
  return element * 10;
})

console.log(remap);
// [10, 20, 30, 40, 50] 출력

let remap = mapping.map(function(element){

  return element * 10;

})

let mapping에 담긴 배열을 한 바퀴 돌면서 각 요소에 곱하기 10을 한 값을 return 해서 remap에 새로운 배열로 담는다.