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
Written By
Supun Kavinda
Written On
Apr 17, 2019
Last Updated On
Apr 17, 2019
Latest on Hyvor Developer
Creating a Real-Time Chat App with PHP and Node.js
All About MYSQLI Prepared Statements in PHP
Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
PHP Contact Form - The Email Method
Related Articles
0