forEach in JS

서재환·2021년 8월 2일
0

JavaScript

목록 보기
1/25

forEach

forEach

배열에 한해 사용할 수 있는 메서드이고 메서드의 인자로 원소, 지수, 배열을 갖는다. 
뒤에서부터 삭제해서 인자를 차례대로 사용할 수 있다. 아래 코드를 보면 이해할 수 있다.
const arr = [1, 2, 3, 4];

arr.forEach((elem, index, Arr) => {
 console.log('elem:', elem);
 console.log('index:', index); 
 console.log('Arr:', Arr);
 console.log('');
});
result

  elem: 1
  index: 0
  Arr: [ 1, 2, 3, 4 ]

  elem: 2
  index: 1
  Arr: [ 1, 2, 3, 4 ]

  elem: 3
  index: 2
  Arr: [ 1, 2, 3, 4 ]

  elem: 4
  index: 3
  Arr: [ 1, 2, 3, 4 ]

0개의 댓글