Since neither the collection object, nor the for.each construct exist in javaScript, we have to simulate this as a method of the collection class, so it can be called as per the following pattern.

co.forEach(function (x) {do something with x});

What's happening here ?

First, in the collection class, I have implemented a method called forEach()

this.forEach = function(yourFunction) {

for (var i = 0 ; i < self.count() ; i++ ) {

yourFunction( self.item(i+pBase), i+pBase) ;

}

};

In javaScript, we can create an anonymous function function (cItem,cIndex) {...} and pass it as an argument to forEach(). The forEach() method then calls back this anonymous function for every item in the collection, passing a couple of arguments, one of which is the current item in the collection as we pass through it. You then simply process the item as you would have done inside the VBA forEach loop.