[JS] [React] Array.prototype.map()

에릭리·2022년 7월 9일
0

TIL

목록 보기
3/8
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

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

구문)

arr.map(callback(currentValue[, index[, array]])[, thisArg])
  • callback - 새로운 배열요소를 생성하는 함수와 그 인수들:
    -currentValue(처리할 현재요소)
    -index(처리할 현재요소의 인덱스)
    -array(map()을 호출한 배열)
  • thisArg - callback을 실행할 때 this로 사용되는 값

내가 쓴 예시)
https://velog.io/@leeg/React-%EB%8C%93%EA%B8%80-%EA%B8%B0%EB%8A%A5-%EA%B5%AC%ED%98%84

0개의 댓글