JavaScript

HTMLCollection - > Array로 바꾸기

ahleum 2022. 2. 23. 23:39

getElementByClassName으로 태그들을 부르게 되면 HTMLCollection으로 나타나는데

이를 array 형식으로 바꾸길 원한다면

Array.from(......) 메소드를 넣어주면 array 형식으로 바뀌게 된다.

 

 

const colors = document.getElementsByClassName("jsColor");


console.log(colors);
console.log(Array.from(colors));

 

만약 처음부터

document.querySelectorAll(".jsColor");

 
주게 된다면 이 방법도 array로 주게 되어 이대로 사용가능하다
 

 

const jsColor = document.querySelectorAll(".jsColor");

console.log(jsColor);
console.log(jsColor[0]);