Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you iterate over an object (associative array) in Dojo?

Tags:

dojo

Does Dojo have a method similar to jQuery's each() that allows you to pass an object to iterate over? jQuery.each() allows you to pass either an array or an object. In the latter case, the callback function receives both a key and the value. Is there something that allows you to do this in Dojo?

like image 773
mydoghasworms Avatar asked Oct 06 '11 15:10

mydoghasworms


1 Answers

Looks like you are looking for dojox.lang.functional.object.forIn.

There's no actual documentation page in dojo reference, only a small example in article Functional fun in JavaScript with Dojo:

Module dojox.lang.functional.object defines important object helpers:

df.forIn(object, callback[, thisObject])

If you have something against using that module you can also easily make your own variant:

function objEach(obj, f, scope){
    for(var key in obj){
        if(obj.hasOwnProperty(key)){
            f.call(scope, obj[key], key);
        }
    }
}

For arrays there is already dojo.forEach() in the base library.

like image 82
hugomg Avatar answered Sep 21 '22 23:09

hugomg