forEach() 메서드는 배열에 활용이 가능한 메서드로, 파라미터로 주어진 함수를 배열 요소 각각에 대해 실행하는 메서드이다.
map() 메서드와 거의 비슷하지만 차이점은 따로 return 값이 없다.
const number = [3, 7, 4, 2];
number.forEach(function (element, index) {
console.log(element, index);
});
// 결과
// 3, 0
// 7, 1
// 4, 2
// 2, 3
const number = [3, 7, 4, 2];
number.forEach((element, index)=>{
console.log(element, index);
});
// 결과
// 3, 0
// 7, 1
// 4, 2
// 2, 3
배열의 start index부터 end index 전까지(end index는 미포함) value값으로 채워주는 함수입니다.
arr.fill(value [, start [, end]])
const arr = [1, 2, 3, 4];
// fill with 0 from position 2 until position 4
console.log(arr.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]
// fill with 5 from position 1
console.log(arr.fill(5, 1));
// expected output: [1, 5, 5, 5]
console.log(arr.fill(6));
// expected output: [6, 6, 6, 6]
console.log(arr.fill()); // fill 인자가 없으므로 undefined 할당됨
// expected output: (4) [undefined, undefined, undefined, undefined]