Array
1.Array.map()
map method는 array를 반복해주는데,
callback 함수에서 return한 값으로 매(each) 요소를 수정해 준다.
map method의 return 값은 수정된 값으로 다시 array를 생성한다.
const arr = [1, 2, 3];
const squares = arr.map(x => x * x);
console.log(squares);
//expected result = [1, 4, 9]
2.Array.forEach()
forEach method는 주어진 함수를 배열 요소 각각에 대해 실행한다.
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
function logArrayElements(element, index, array) {
console.log('a[' + index + '] = ' + element);
}
[2, 5, , 9].forEach(logArrayElements);