arrow function은 callback 함수로 사용시 가장 많이 사용함.
📌 Array.map()
📌 Array.forEach()
map 메서드는 배열을 반복해주며, callback함수에서 리턴한 값으롬 각(each)요소를 수정.
map 메서드의 리턴값은 수정된 값으로 다시 생성된 배열.
const arr = [1, 2, 3];
const squares = arr.map(x => x * x);
console.log(arr) //[1, 2, 3]
console.log(squares) //[1, 4, 9]
for대신 사용하는 forEach 반복문
리턴값이 없고, forEach함수 return 사용 탈출가능
let startWithNames = [];
let names = ['a', 'ab', 'cbb', 'ada'];
names.forEach(el => {
if (el.startsWith('a')) {
startWithNames.push(el);
}
});
startWithNames //결과 : [ 'a', 'ab', 'ada' ]