Skip to content

Instantly share code, notes, and snippets.

@fschell
Last active September 15, 2015 13:58
Show Gist options
  • Save fschell/5bbb704fe9b26137166b to your computer and use it in GitHub Desktop.
Save fschell/5bbb704fe9b26137166b to your computer and use it in GitHub Desktop.
NodeList to Array
// create array from NodeList without modifying NodeList Prototype
var arr = Array.prototype.slice.call( document.querySelectorAll('div') );
// extend NodeList prototype...
NodeList.prototype.array = function() {
return Array.prototype.slice.call(this);
};
// ...and use it as chained method
var arr = document.querySelectorAll('div').array();
// now we can use forEach
arr.forEach(function(e,i){
console.log(e)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment