Skip to content

Instantly share code, notes, and snippets.

@bendc
Created January 13, 2015 14:39
Show Gist options
  • Save bendc/4090e383865d81b4b684 to your computer and use it in GitHub Desktop.
Save bendc/4090e383865d81b4b684 to your computer and use it in GitHub Desktop.
ES6: Iterating over a NodeList
var elements = document.querySelectorAll("div"),
callback = (el) => { console.log(el); };
// Spread operator
[...elements].forEach(callback);
// Array.from()
Array.from(elements).forEach(callback);
// for...of statement
for (var div of elements) callback(div);
@varkadov
Copy link

varkadov commented Aug 1, 2016

Array.from(elements, callback)

@treeskar
Copy link

treeskar commented Sep 1, 2016

You also can add custom iterator

elements[Symbol.iterator]();
elements.forEach(callback)

@mzalazar
Copy link

mzalazar commented Jan 3, 2019

[].forEach.call(elements, callback)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment