Array - map()

sorikikikim·2023년 5월 12일

Javascript-Array

목록 보기
5/5

Array.prototype.map()

map() 메서드는 배열 내의 모든 요소 각각에 대하여 주어진 함수(조건)를 호출한 결과를 모아 새로운 배열을 반환한다.

const number = [1, 2, 3, 4, 5];

// Pass a function to map
const newNumber = number.map(n => n * 2);

console.log(newNumber);
// Expected output: Array [2, 4, 6, 8, 10]

map()에 대한 사용법은 다음과 같다.
arr.map(callback(currentValue[, index[, array]])[, thisArg])
map()은 배열의 각 요소에 대해 실행한 callback의 결과를 모은 새로운 배열을 반환한다.

  • callback
    새로운 배열 요소를 생성하는 함수. 다음 세 가지 인수를 가진다.
    - currentValue
    처리할 현재 요소.
    - index (Optional)
    처리할 현재 요소의 인덱스.
    - array (Optional)
    map()을 호출한 배열.
  • thisArg (Optional)
    callback을 실행할 때 this로 사용되는 값.

0개의 댓글