HTMLs Tutorial

HTML Tutorial HTML Tags HTML Basic Tags HTML Attributes HTML Elements HTML Formatting HTML Text Format HTML body tag HTML samp tag HTML script Tag HTML section tag HTML select tag HTML source tag HTML span tag HTML strike tag HTML strong tag HTML style tag HTML sub tag HTML summary tag HTML sup Tag HTML svg tag HTML table tag HTML u tag HTML Headings HTML Paragraphs HTML wbr tag HTML Anchor HTML Image HTML Lists HTML Ordered List HTML Unordered List HTML Form HTML Form input HTML with CSS HTML Layouts HTML References HTML Frames HTML Links Fieldset Tag in HTML Basic HTML Tags Br Tag in HTML Free HTML Templates How to Create a Table in HTML HTML Calendar HTML Card HTML Cellspacing HTML Center Image HTML Checkbox Read-only HTML Cleaner HTML Code for a Tab HTML Comment HTML Compiler HTML Nested Forms HTML Overlay Text on the Image HTML Select Option Default HTML Snake Game HTML Subheader HTML Tab Character dd Tag in HTML How Many HTML Tags are There HTML Align Tag HTML Responsive HTML Tab Code HTML Table Alternate Row Color HTML Table Fix Column Width Contact HTML DL Tag in HTML How to Insert Image in HTML HTML Background Color HTML Dark Mode How to Convert HTML to PNG HTML Data Toggle HTML Email Template HTML Font Color HTML Font Family ID and Class in HTML HTML Tab Space HTML Tab Tag HTML Itemprop HTML Itemscope HTML Form Design HTML Input Only Numbers HTML Textarea HTML to JPG HTML to Markdown Python li Tag in HTML MDN HTML What is the Correct HTML for Making a Hyperlink? What is the Root Element of an HTML Document How to Make a Box in HTML How to Save HTML Files in Notepad How to Align Text in HTML How to Change Font Color in HTML? How to Change Font Size in HTML How to Change Image Size in HTML How to Create a HTML Page How to Create a Link in HTML File? How to Create an HTML File? HR Tag in HTML HTML Base Tag HTML Default Attribute HTML Hyperlink HTML Indent HTML Injection Payloads HTML Input Numbers Only HTML Roadmap HTML Row Height HTML Schedule HTML Space HTML Tab HTML vs HTTP HTML5 API HTML5 Video HTML Collection to Array Text Area in HTML

HTML5 Advance

HTML5 Tutorial HTML5 Tags HTML Button Tag HTML canvas Tag HTML caption Tag HTML City tag HTML Tag HTML5 SVG HTML Event Attribute HTML5 Audio HTML5 Youtube HTML5 Button Tag HTML5 Tags

Misc

How to add JavaScript to HTML How to change font in HTML How to change text color in HTML HTML Date HTML Hide Element HTML Nested Table HTML Reset Button What does HTML stand for? HTML Background Image HTML Tag Div Tag in HTML How to insert Image in HTML How to create a link with no underline in HTML How to insert spacestabs in text using HTMLCSS HTML tag HTML Code HTML Tag HTML Canvas Design a tribute page using HTML and CSS What is a Container tag Font tag in HTML Difference between HTML and DHTML Empty Tag in HTML HTML Button Link Html Line Break Img src HTML Nested List in HTML Placeholder in HTML TD in HTML HTML Space Code HTML Target Attribute HTML Tag Markup Meaning in HTML Border-Collapse in HTML HTML Onclick Online HTML Compiler Convert HTML to PDF HTML Formatter HTML5 - Web Storage HTTP – Responses Container Tag in HTML DL Tag in HTML Horizontal Rule HTML HTML Tab Text Html Table Cell Background Color HTML Table Cell Color HTML Col Width How Many HTML Tags are There Convert String to Unicode Characters in Python HTML Runner HTML Style Attribute HTML Superscript Attribute HTML tabindex Marquee Tag in HTML HTML Dynamic Form HTML side Tag HTML Pattern Attribute HTML q Tag HTML Readonly Base 64 Encoding in HTML Documents Enhancing Data Portability and Security Evo Cam Web Cam HTML Free code camp HTML CSS How to Add a JS File in HTML? How to Add Picture in HTML How to Add the Logo in HTML? How to Add Video in HTML HTML Class Attribute HTML Entities HTML Form Elements HTML Form Templates HTML Marquee Tag HTML Radio Buttons HTML Text box HTML to JSX HTML Tooltip Basic HTML Codes How to Align Image Center in HTML HTML Header Tag HTML Image Tag HTML Next Line

HTML Collection to Array

The most Effective Method for Changing an HTML Collection into an Array

Several DOM methods, such as "getElementsByTagName" and "querySelectorAll," return an HTMLCollection, which is an array-like collection of HTML elements. Nevertheless, this collection lacks the functionality of an array and is not an array itself. For this reason, to carry out some array-specific actions, it might be required to transform the HTMLCollection to an array. In this post, we'll look at every way to turn an HTMLCollection into an array.

Table of Content

  • Using Array. prototype.slice( )
  • Using Array.from()
  • Using Spread Operator
  • Using for-loop

Using Array.prototype.slice(): 

In this technique, Array.prototype.slice() is bound to the call() function. This will cause HTMLCollection to use Array.prototype.slice(), which transforms it into an array by making a shallow duplicate of the HTMLCollection. The syntax we'll use is listed below.

Syntax:

Array.prototype.slice.call(htmlCollection)

What is HTMLCollection Array?

First Solution:

It is an HTMLCollection rather than an Array.

It does not inherit from the Array prototype since it has Array-like attributes like numeric properties and an a.length property. As such, it is unique in that it lacks the conventional Array methods.

It is noteworthy that HTMLCollection is a dynamically updated collection that takes into account DOM changes. Upon removal from the DOM, a node is immediately deleted from HTMLCollection.

Option 2:

If you are thinking of converting an array, please read this post.

The best way to convert an HTMLCollection into an array.

Throughout the debate, many approaches were presented, and I discovered that the solution in the chosen response also addressed a problem similar to one I had faced.

How may HTMLCollection be iterated upon? [Repeated], Simply use the spread operator [Array.from(document.getElementsByClassName('node-item')) or the spread operator.

Why not just Turn Them into an Array?

To utilize higher-level methods like forEach, map, filter, and reduce, you must convert an HTMLCollection and NodeList to an array.

There are several applications. For example, suppose you have components with a data-src attribute and a lazy-load class. You may use the following to obtain the data-src and filter out all the items that are empty or lack data-src.

Use Case

var lazyLoadables = [...document.querySelectorAll('.lazy-load')]

  .filter((element) => element.getAttribute('data-src').trim());

lazyLoadImages(lazyLoadables);

We ensured that only items with a source that must be loaded when necessary were sent by doing this.

<!DOCTYPE html>

<html>

<head>

<title>Example for HTMLCollection To Array Conversion</title>

</head>

<body>

<div>

<p>This is line 1</p>

<p>This is line 2</p>

<p>This is line 3</p>

</div>

<script>

const htmlCollection =

document.getElementsByTagName("p");

const array = Array.prototype

.slice.call(htmlCollection);

console.log(array);

</script>

</body>

</html>

Output

HTMLCollection to Array/>
<!-- /wp:html -->

<!-- wp:heading {

Using Array.from(): 

ES6 introduces the Array.from() function. This function creates a new array with the same items by taking an iterable object that resembles an array. In this instance, HTMLCollection is the array-like iterable object. All we have to do is feed it to this function, and it returns an array.

Syntax:

Array.from(htmlCollection)

Example: In this example, an HTMLCollection is converted to an Array in an HTML page using the Array.from() function.

<!DOCTYPE html>

<html>

<head>

<title>Example for HTMLCollection to Array Conversion</title>

</head>

<body>

<div>

<p>This is line 1</p>

<p>This is line 2</p>

<p>This is line 3</p>

</div>

<script>

const htmlCollection =

document.getElementsByTagName("a");

const array = Array.from(htmlCollection);

console.log(array);

</script>

</body>

</html>

Output

HTMLCollection to Array/>
<!-- /wp:html -->

<!-- wp:heading {

Spread Operator: 

We can also convert an HTML Collection to an array by using spread opera. An iterable object can be expanded into numerous components using the spread operator(...). We may make an array that has the same element as HTMLCollection by using the spread syntax enclosed in square brackets.

Syntax:

[...htmlCollection]

Example: In this example, an HTML collection is converted to an Array on an HTML page using the spread operator.

<!DOCTYPE html>

<html>

<head>

<title>Example for HTMLCollection to Array Conversion</title>

</head>

<body>

<div>

<p>This is line 1</p>

<p>This is line 2</p>

<p>This is line 3</p>

</div>

<script>

const htmlCollection =

document.getElementsByTagName("p");

const array = [...htmlCollection];

console.log(array);

</script>

</body>

</html>

Output

HTMLCollection to Array/>
<!-- /wp:html -->

<!-- wp:heading {

Using for-loop

The most popular method for converting an HTMLCollection to an array is to use a for-loop. Here, all we have to do is loop through the HTMLCollection, pushing each piece into a separate array.

Syntax:

const array = [];

for (let i = 0; i < collection.length; i++) {

  array.push(collection[i]);

}

Example: In this example, an HTMLCollection is converted to an Array in an HTML page using the for loop technique.

<!DOCTYPE html>

<html>

<head>

<title>Example for HTMLCollection to Array Conversion</title>

</head>

<body>

<div>

<p>This is line 1</p>

<p>This is line 2</p>

<p>This is line 3</p>

</div>

<script>

const collection = document.getElementsByTagName('p');

const array = [];

for (let i = 0; i < collection.length; i++) {

array.push(collection[i]);

}

console.log(array);

</script>

</body>

</html>

Output

HTMLCollection to Array/>
<!-- /wp:html -->

<!-- wp:html -->
<div class=