tweets에 forEach는 되는데, reduce는 안되는 이유

Jelkov Ahn·2021년 9월 10일
0

DOM

목록 보기
7/14
post-thumbnail

forEach 메서드

  • Array.prototype.forEach()

  • forEach() 메서드는 주어진 함수를 배열 요소 각각에 대해 실행합니다.

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"

reduce가 안되는 이유

Nodelist는 배열이 아니라, 노드의 콜렉션입니다.
그래서 배열 메소드들을 사용할 수 없으며, Nodelist에 사용가능한 메소드들은 Nodelist.item(), Nodelist.entries(), Nodelist.forEach(), Nodelist.keys(), Nodelist.values() 가 있습니다.

즉 배열 메소드를 사용할 수 없기 때문에 reduce는 사용이 불가하지만, forEach는 Nodelist의 메소드이기도 하기 때문에 사용이 가능합니다.

출처:
(1) https://developer.mozilla.org/ko/docs/Web/API/NodeList
(2) https://stackoverflow.com/questions/31338097/why-it-is-not-possible-to-call-foreach-on-a-nodelist

profile
끝까지 ... 가면 된다.

0개의 댓글