Refactor 'for each ...' into just for loops.

The 'for each' statement is not part of the ECMAScript standard, as such it's
not supported by all major javascript engines (such as Google's V8).  It's also
been deprecated to discourage it's use.  See
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for_each...in
for more information.

This patch refactors its usage into normal javascript for loops.