꾸준히 성장하는 개발자

HTMLCollection - > Array로 바꾸기 본문

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]);

'JavaScript' 카테고리의 다른 글

[JavaScript] 시계 만들기  (0) 2022.02.28
물음표 연산자 , 삼항연산자 , 조건부 연산자  (0) 2022.02.27
var , let , const  (0) 2021.12.28
Youtube iframe API  (0) 2021.12.28
node.js 준비하기  (0) 2021.12.24