Simple forEach implementation for Objects NodeLists Arrays blog post

Simple forEach implementation for Objects NodeLists Arrays

Todd Motto

14 May, 2014

3 minutes read

Looping Objects is easy. Looping Arrays is also easy. Looping NodeLists is easy. They can be a little repetitive though and often take time to construct each loop and pass in the index, property, element or whatever…

There is no “standard” way of iterating over everything. We can use Array.prototype.forEach to loop over Arrays (or the regular for loop), a for in loop for Objects, and a regular for loop again for NodeLists or HTML collections. No, you’re not going to use that forEach.call(NodeList) hack.

Wouldn’t it be nice to just forget about what type of collection we’re looping, forget about browser support and write a nice little function that handles everything for us. Yes.

So I did…

forEach.js

forEach.js is a simple script, it’s not part of a library or even a module, it’s just a function, here’s its syntax and a quick example using an Array:

// syntax
forEach(collection[, callback[, context]]);

// example
var myArray = ['A', 'B', 'C', 'D'];
forEach(myArray, function (value, index) {
    // `this` will reference myArray: []
}, myArray); // note third param changing execution context

forEach() for Arrays/NodeLists

You can loop over an Array or NodeList using a standard for loop, however, NodeLists cannot be used in conjunction with the newer ECMAScript 5 Array.prototype.forEach. This script takes care of that in the same way it loops over an Array, you’ll get the same stuff passed back:

// Array:
forEach(['A', 'B', 'C', 'D'], function (value, index) {
    console.log(index); // 0, 1, 2, 3
    console.log(value); // A, B, C, D
});
// NodeList:
forEach(document.querySelectorAll('div'), function (value, index) {
    console.log(index); // 0, 1, 2, 3
    console.log(value); // <div>, <div>, <div>...
});

forEach() for Objects

Object iteration is usually done via a for in loop, we can wrap this up by passing back values which makes our loops cleaner and easier to manage:

// Object:
forEach({ name: 'Todd', location: 'UK' }, function (value, prop, obj) {
    console.log(value); // Todd, UK
    console.log(prop); // name, location
  console.log(obj); // { name: 'Todd', location: 'UK' }, { name: 'Todd', location: 'UK' }
});

collection

Type: Array|Object|NodeList

Collection of items to iterate, could be an Array, Object or NodeList.

callback

Type: Function

Callback function for each iteration.

context

Type: Array|Object|NodeList Default: null

Object/NodeList/Array that forEach is iterating over, to use as the this value when executing callback.

Code

For those interested, check out the code below, the latest version is available on GitHub.

var forEach = function (collection, callback, scope) {
  if (Object.prototype.toString.call(collection) === '[object Object]') {
    for (var prop in collection) {
      if (Object.prototype.hasOwnProperty.call(collection, prop)) {
        callback.call(scope, collection[prop], prop, collection);
      }
    }
  } else {
    for (var i = 0; i < collection.length; i++) {
      callback.call(scope, collection[i], i, collection);
    }
  }
};

About the author

Todd Motto

GDE Google Developer Expert

Todd is the Founder of Ultimate Courses. With a passion for Angular, TypeScript and JavaScript, Todd leads the online courses creation and has written hundreds of articles on front-end web development and beyond. He specialises in breaking down complex topics and understands the critical mission of learning new technology fast, comprehensively and the right way.

Love the post? Share it!

Lots of time and effort go into all our blogs, resources and demos,
we'd love it if you'd spare a moment to share them!

Explore our JavaScript courses

Get started today and join over 50,000 developers.