Last active
August 29, 2015 14:13
-
-
Save eljefedelrodeodeljefe/d67489e9814d64a37063 to your computer and use it in GitHub Desktop.
Convert node-lists to arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is my approach to convert node-lists in order to apply Array.prototype methods like .forEach |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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