dojo.forEach

Status:Contributed, Draft
Version:1.0

Since it’s not supported natively on every browser, dojo.forEach provides the standard JavaScript 1.6 forEach construct everywhere. dojo.forEach lets you apply a function to each element of an array, emulating a for loop, but with fewer scoping compliations.

dojo.forEach has a notable difference from the JavaScript 1.6 forEach: dojo.forEach runs over sparse arrays, passing the “holes” in the sparse array to the callback function. JavaScript 1.6’s forEach skips the holes in the sparse array.

dojo.forEach() cannot terminate a loop (save for throwing an exception). Use dojo.some() or dojo.every() instead.

forEach is syntactic sugar for a regular ‘ol for loop:

1
2
3
for(var i=0; i<queueEntries.length; i++){
  console.debug(queueEntries[i], "at index", i);
}

Can be written as:

1
2
3
dojo.forEach(queueEntries, function(entry, i){
  console.debug(entry, "at index", i);
});

We use an anonymous function to define the operation. This function is always passed 3 arguments: the current item value, the index in the list being iterated over, an a reference to the list itself.

For this simple loop, dojo.forEach isn't anything exciting. But combined with other Dojo functions, especially dojo.query(), it becomes remarkably useful. Consider this snippet, which disables all <select> tags on the page:

1
2
3
4
5
6
dojo.forEach(
  dojo.query("select"),
  function(selectTag) {
    selectTag.disabled = true;
  }
);

How cool is that? (Answer: very!) There's no monkeying around with DOM functions, no using tedious names or id's, and it continues to work even when you add new SELECT tags.

Running dojo.forEach on a dojo.query result is so common, that Dojo defines a shortcut:

1
2
3
dojo.query("select").forEach(function(selectTag){
    selectTag.disabled = true;
});

does the same thing. But that's not all! New in 1.0 you can collapse the function down to its body, passed in as a string like so:

// 1.0 only.

1
dojo.query("select").forEach("item.disabled = true;");

Ay carumba! That's a lot of functionality in a tiny 1-line package. Once you get used to the syntax, you'll never want to go back.

See Also

  • dojo.map - The Dojo version of Array.map
  • dojo.filter - Helps you narrow down the items in a list
  • dojo.some - Does any item in the list meet your critera?
  • dojo.every - Do all items in the list meet your critera?
  • dojo.indexOf - Find something in a list easily
  • dojo.lastIndexOf - Find something in the list easily, but starting from the back
  • dojo.query - A CSS query engine that returns instances of dojo.NodeList
  • dojo.NodeList - A subclass of Array which can also have forEach applied.

Table of Contents

Error in the documentation? Can’t find what you are looking for? Let us know!