map() method는 array를 return 합니다. return된 배열은 array의 각 요소에 대하여 callback 함수가 return한 값 입니다.
const arr = [7, 1, 9, 13];
const mapArr = arr.map(x => x * 2);
console.log(mapArr);
// expected output: Array [14, 2, 18, 26]
const arr = [7, 1, 9, 13];
const mapArr = arr.map(function (x) {
return x * 2;
});
console.log(mapArr);
// expected output: Array [14, 2, 18, 26]
Array.forEach( ) method는 array 각 요소에 대하여 callback 함수를 실행 합니다. 인자가 1개 일 때에는 요소값을 사용할 수 있고, 인자가 2개 일 때에는 요소값과 index값을 사용할 수 있습니다.
const arr = ['a', 'b', 'c'];
arr.forEach(element => console.log(element));
// expected output: a
// expected output: b
// expected output: c
const arr = ['a', 'b', 'c'];
arr.forEach((element,index) => console.log(element,index));
// expected output: a0
// expected output: b1
// expected output: c2