tweets를 유사 배열에서 배열로 바꾸는 방법

Jelkov Ahn·2021년 9월 10일
0

DOM

목록 보기
8/14
post-thumbnail

tweets를 유사 배열에서 배열로 바꾸는 방법(how to convert nodelist into javascript array)

NodeList는 DOM 요소의 컬렌션이나 또는 특별한 노드를 나타내는 유사배열 객체입니다.
배열과 동일하지만,
NodeList 개체에서 map(), slice() 및 filter()와 같은 일반적인 배열 메서드는 사용할 수 없습니다.

다음은 NodeList 개체를 JavaScript의 배열로 변환하는 다양한 방법
(1) Array.from() Method

// create a `NodeList` object
const divs = document.querySelectorAll('div');

// convert `NodeList` to an array
const divsArr = Array.from(divs);

(2) Spread Operator

// create a `NodeList` object
const divs = document.querySelectorAll('div');

// convert `NodeList` to an array
const divsArr = [...divs];

(3) Array.prototype.slice() Method

// create a `NodeList` object
const divs = document.querySelectorAll('div');

// convert `NodeList` to an array
const divsArr = Array.prototype.slice.call(divs);

출처: https://attacomsian.com/blog/javascript-convert-nodelist-to-array

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

0개의 댓글