Login Register Help
API Documentation
Choose a stylesheet:

Functiondojo.forEach

This function corresponds to the JavaScript 1.6 Array.forEach() method. For more details, see: http://developer.mozilla.org/en/docs/CoreJavaScript1.5Reference:GlobalObjects:Array:forEach

parametertypedescription
arrArray|Stringthe array to iterate over. If a string, operates on individual characters.
callbackFunction|Stringa function is invoked with three arguments: item, index, and array
thisObjectObjectOptional. may be used to scope the call to callback

Usage

var foo=dojo.forEach(arr: Array|String, callback: Function|String, thisObject: Object?);

Examples

Example 1

// log out all members of the array:
dojo.forEach(
    [ "thinger", "blah", "howdy", 10 ],
    function(item){
        console.log(item);
    }
);

Example 2

// log out the members and their indexes
dojo.forEach(
    [ "thinger", "blah", "howdy", 10 ],
    function(item, idx, arr){
        console.log(item, "at index:", idx);
    }
);

Example 3

// use a scoped object member as the callback
 
 
var obj = {
    prefix: "logged via obj.callback:",
    callback: function(item){
        console.log(this.prefix, item);
    }
};
 
 
// specifying the scope function executes the callback in that scope
dojo.forEach(
    [ "thinger", "blah", "howdy", 10 ],
    obj.callback,
    obj
);
 
 
// alternately, we can accomplish the same thing with dojo.hitch()
dojo.forEach(
    [ "thinger", "blah", "howdy", 10 ],
    dojo.hitch(obj, "callback")
);