Skip to content

Commit ecb6c69

Browse files
committed
Array.from - DOM Elements(NodeList)
1 parent a8cd8f4 commit ecb6c69

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

app.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
// from - returns Array Object from any object with a length property or an iterable object
44
// from turns array-like/ish into array - string,nodeList,Set
55

6-
/* String */
7-
const person = "peter";
8-
console.log(Array.from(person));
6+
const paragraphs = document.querySelectorAll("p");
7+
const result = document.getElementById("result");
8+
const second = document.getElementById("second");
99

10-
/* Arguments */
11-
function countTotal() {
12-
console.log(arguments);
13-
let total = Array.from(arguments).reduce(
14-
(total, current) => (total += current)
15-
);
16-
console.log("Total:", total);
17-
}
18-
countTotal(20, 30, 40, 100);
10+
let text = Array.from(paragraphs);
11+
text = text
12+
.map((item) => {
13+
return `<span>${item.textContent}</span>`;
14+
})
15+
.join(" ");
16+
result.innerHTML = text;
17+
18+
const newText = Array.from(document.querySelectorAll("p"), (item) => {
19+
return `<span>${item.textContent}</span>`;
20+
}).join(" ");
21+
second.innerHTML = newText;

index.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
<link rel="stylesheet" href="./styles.css" />
1212
</head>
1313
<body>
14-
<h1>ES6</h1>
15-
<h1>JavaScript</h1>
16-
<h1>CSS</h1>
17-
<h1>HTML</h1>
14+
<h1 style="text-align: center">ES6</h1>
15+
<p>john</p>
16+
<p>susan</p>
17+
<p>peter</p>
1818
<h2 id="result"></h2>
19+
<h1 id="second"></h1>
20+
1921
<!-- Link to JavaScript -->
2022
<script src="./app.js"></script>
2123
</body>

styles.css

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ body {
55
}
66

77
h1 {
8-
text-align: center;
98
letter-spacing: 2px;
109
}
1110

0 commit comments

Comments
 (0)