Convert NodeList to Array

This entry was posted by Thursday, 14 March, 2013

Now that most browsers have implemented querySelectorAll, the native selectorEngine, many framework-dependent developers are getting a rude awakening when dealing with the result of QSA calls:  the NodeList object.  NodeLists are array-like but don’t feature many of the methods provided by the Array, like forEach, map, filter, etc.  JavaScript does, however, provide a very simple way to convert NodeLists to Arrays:

var nodesArray = Array.prototype.slice.call(document.querySelectorAll("div"));

The result of the code above is a true Array object containing all of the nodes returned by the QSA.  You could even make the code shorter with this alternative:

var nodesArray = [].slice.call(document.querySelectorAll("div"));

Both snippets will give you an Array for which you can iterate over and do all those other awesome array things!

Read the full article at: Convert NodeList to

Read Full Post at David Walsh Blog