To-array conversion in JavaScript
Let's have a pseudo-array of paragraphs:
javascriptlet elems = document.querySelectorAll('p');
Let's convert it to a regular array.
The first way
The most obvious thing to do is to iterate over our pseudo-array in a loop, forming a new array in this loop:
javascriptlet arr = [];
for (let elem of elems) {
arr.push(elem);
}
The second way
We can use destructuring:
javascriptlet arr = [...elems];
The third way
We can use Array.from method:
javascriptlet arr = Array.from(elems);
Practical tasks
Задание 1
Somehow get a pseudo-array of elements. Convert it to an array in the three ways described.
Задание 2
Paragraphs are given:
javascriptlet elems = document.querySelectorAll('p');
Use the slice method to get
all the paragraphs except the first
and last.