Skip to content

Instantly share code, notes, and snippets.

@eljefedelrodeodeljefe
Last active August 29, 2015 14:13
Show Gist options
  • Save eljefedelrodeodeljefe/d67489e9814d64a37063 to your computer and use it in GitHub Desktop.
Save eljefedelrodeodeljefe/d67489e9814d64a37063 to your computer and use it in GitHub Desktop.
Convert node-lists to arrays
This is my approach to convert node-lists in order to apply Array.prototype methods like .forEach
function queryNodes( selectors ) {
// push all node objects into this array
var arrayOfNodes = [];
// FIXME: options object is referenced from class, not applicable here
var selectors = this.opts.targetSelector;
// accept multiple selectos
selectors.forEach( function( selector ) {
var nodeList = document.querySelectorAll(selector);
// convert the nodeList-Object to a regular array
var nodes = Array.prototype.slice.call(nodeList);
nodes.forEach( function( node ) {
arrayOfNodes.push( node );
});
})
// return array to use at others places
return arrayOfNodes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment