본문 바로가기

ES6

Array.from() - 매우 중요

반응형

See the Pen 200918-7 by hwd3004 (@hwd3004) on CodePen.

 

 

Array.of()는 어떤걸 array로 만들고 싶을때 사용함.

 

 

 

 

 

 

 

 

Array.from()을 알아보기전에

 

See the Pen 200918-8 by hwd3004 (@hwd3004) on CodePen.

console.log(buttons) 에서는 NodeList가 뜬다.

array인거 같지만 array는 아닌 NodeList이다.

 

console.log(btn) 에서는 HTMLCollection이란게 나온다.

이것도 array인거 같지만 array는 아니다.

 

이런걸 array-like obejct라고 부른다.

 

각각의 버튼들을 가져와서 이벤트리스너를 붙이려할때 forEach로 하려고 하면

forEach는 function이 아니라는 에러가 뜬다.

buttons, btn이 forEach 메소드를 가지고 있지 않은 것이다.

왜? array가 아닌 array 비슷한 것이므로.

 

vscode에서 Array.from 을 입력하고 설명을 보면

Creates an array from an array-like object. 이라고 보여준다.

 

 

 

 

See the Pen 200918-8 by hwd3004 (@hwd3004) on CodePen.

 

매우 잘된다.

 

 

 

 

See the Pen 200918-9 by hwd3004 (@hwd3004) on CodePen.

 

이렇게도 가능.

반응형

'ES6' 카테고리의 다른 글

Object Destructuring  (0) 2020.09.18
Array.findIndex(), Array.fill()  (0) 2020.09.18
string 메소드 - includes(), repeat(), startsWith(), endsWith()  (0) 2020.09.18
Cloning Styled Components  (0) 2020.09.18
HTML Fragments  (0) 2020.09.17