프론트엔드 첫걸음

NodeList와 Array의 차이점 본문

개발 공부/Javascript

NodeList와 Array의 차이점

차정 2022. 4. 3. 15:07

 

NodeList와 Array 차이점

1.  NodeLists는 DOM 요소에 접근하는 언어에 구애받지 않는 객체(브라우저 API)이며,
Arrays는 항목 모음을 포함하는 데 사용할 수 있는 JavaScript 객체다.

2.  NodeList는 정적 콜렉션, Node.childeNodes는 동적인 콜렉션이다.

경우에 따라, NodeList는 라이브 콜렉션으로, DOM의 변경 사항을 실시간으로 콜렉션에 반영합니다. 예를 들어, Node.childNodes 는 실시간입니다:

var parent = document.getElementById('parent');
var child_nodes = parent.childNodes;
console.log(child_nodes.length); // let's assume "2"
parent.appendChild(document.createElement('div'));
console.log(child_nodes.length); // should output "3"

 

다른 경우 NodeList는 정적 콜렉션입니다. DOM을 변경해도 콜렉션 내용에는 영향을 주지 않습니다. document.querySelectorAll() 은 정적 NodeList를 반환합니다.

 

Don't use for...in to enumerate the items in NodeLists, since they will also enumerate its length and item properties and cause errors if your script assumes it only has to deal with element objects. Also, for...in is not guaranteed to visit the properties in any particular order.
for...of loops loop over NodeList objects correctly:

nodeList를 순회할때 for in 사용하지 말고 for문이나 for of 사용해라!!!!!!!!!!!!!!!!!!!!

 

https://developer.mozilla.org/en-US/docs/Web/API/NodeList

https://developer.mozilla.org/ko/docs/Web/API/NodeList

 

NodeList - Web API | MDN

NodeList 객체는 일반적으로 element.childNodes와 같은 속성(property)과 document.querySelectorAll 와 같은 메서드에 의해 반환되는 노드 (en-US)의 콜렉션입니다.

developer.mozilla.org




 

 

 

 

https://gomakethings.com/converting-a-nodelist-to-an-array-with-vanilla-javascript/

 

Converting a NodeList to an array with vanilla JavaScript

The native JavaScript ES6 release brought a handful of helpful methods for working with arrays: Array.forEach(), Array.every(), Array.some(), Array.filter(), and more. Unfortunately, you can’t use any of these with the elements you get back when using qu

gomakethings.com

유사배열객체 NodeList를 Array로 바꾸려면 ?

let myArray = Array.from(nodeList)

 

Array.from이나 전개구문을 사용하면 됨

 

See the Pen nodelist에 for of쓰는거 아니야 by JEONG (@cona) on CodePen.

 

 

* inputbox에 checked='false'를 했는데 안먹는 이유는??
기냥 checked를 넣으면 그게 check다..  checked='false'쓰지마라...
https://stackoverflow.com/questions/24514566/checkbox-set-to-checked-false-not-working