Last active
August 24, 2021 00:49
-
-
Save emayom/b21eff79cd1a10f92048429a61c6aaf2 to your computer and use it in GitHub Desktop.
[ES6] Array.from() NodeList -> object Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
</head> | |
<body> | |
<ul> | |
<li>apple</li> | |
<li>orange</li> | |
<li>banana</li> | |
<li>strawberry</li> | |
</ul> | |
<script> | |
let lists = document.querySelectorAll("li"); | |
function print() { | |
lists = Array.from(lists) | |
.filter((el) => el.innerHTML.includes("e")) | |
.map((el) => el.innerHTML); | |
return console.log(lists, toString.call(lists)); | |
} | |
print(); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//요소 모두를 NodeList 형태로 가져옴 | |
let lists = document.querySelectorAll("li"); | |
//typeof로는 NodeList 임을 확인할 수 없다. toString.call()으로 확인! | |
console.log(typeof lists); //object | |
console.log(toString.call(lists)); //[object NodeList] | |
function print() { | |
//NodeList를 복사해서 Array로 생성 후 확인 | |
let lists_Array = Array.from(lists); | |
console.log(toString.call(lists_Array)); | |
//filter()를 이용해 내부에 "e"라는 문자를 가진 요소 배열을 eArray에 담음 | |
let eArray = lists_Array.filter((el) => el.innerText.includes("e")); | |
console.log(eArray); | |
return eArray; | |
} | |
print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment