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
You can connect with me on Twitter or Linkedin.
Latest on My Blog
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Laravel High CPU Usage Because of File-based Session Storage
Laravel High CPU Usage Because of File-based Session Storage
Image for Resizing Droplets: A Personal Experience
Resizing Droplets: A Personal Experience
Image for Moving our CDN (10+ GB images) to a new server
Moving our CDN (10+ GB images) to a new server
Image for Disqus, the dark commenting system
Disqus, the dark commenting system
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Related Articles
20