js

[javascript] 유사 배열 객체

beaksul 2022. 10. 25. 16:47

1. 유사 배열 객체

 

배열처럼 보이지만 key가 숫자고 length 값을 가지고 있는 객체

querySelectorAll이나 document.body.children으로 엘리먼트를 가져오면 유사 배열 객체로 나타남

 

 

2. 배열과의 차이점

 

배열에서 유용하게 사용 가능한

  • forEach
  • map
  • filter
  • reduce

같은 메서드를 쓸 수 없음

 

 

3. Array.from()

 

Array.from()으로 유사 배열 객체의 value를 복사해서 배열로 만들면 위 메서드를 정상적으로 사용 가능

const hi = document.querySelectorAll('.hi');
Array.from(hi).map((hi) => console.log(text));