HTMLCollection to Array in Javascript

To perform Array functions like map, filter on a HTMLCollection you will need to convert it to an Array first. Here's how to do it easily in Javascript.

HTMLCollection to Array


var collection = document.getElementsByTagName("div"),
    arr;
// long version
arr = Array.prototype.slice.call(collection);
// short version
arr = [].slice.call(collection);
// ES6 version
arr = [...collection]
Tagged: Javascript
Liked the article? Follow me on Twitter, or help Hyvor grow by funding Hyvor Talk.
Latest on Hyvor Developer
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Image for All About MYSQLI Prepared Statements in PHP
All About MYSQLI Prepared Statements in PHP
Image for Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image for PHP Contact Form - The Email Method
PHP Contact Form - The Email Method
Image for The Best Way to Perform MYSQLI Prepared Statements in PHP
The Best Way to Perform MYSQLI Prepared Statements in PHP
Related Articles
0