Array.map()
각 항목을 변환한 결과로 새 배열을 만듭니다.
기본 문법
items.map((value, index) => result)작은 예제 javascript
const prices = [100, 200, 300];
const doubled = prices.map(price => price * 2);
console.log(JSON.stringify(doubled));
console.log(JSON.stringify(prices));실행 결과
[200,400,600] [100,200,300]
헷갈리기 쉬운 점
- 콜백에서 값을 반환하지 않으면 해당 결과는 undefined가 됩니다.
- 새 배열을 만들지만 객체 항목 내부까지 자동 복사하지는 않습니다.