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

江枫思渺然 提交于 2019-12-22 05:33:19

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7676561/how-can-you-iterate-over-an-object-associative-array-in-dojo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!