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

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

+6
source share
1 answer

It looks like you are looking for dojox.lang.functional.object.forIn .

There is no actual documentation page in the dojo link, just a small example in the article Functional Fun in JavaScript with Dojo :

The dojox.lang.functional.object module defines important object helpers:

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

If you have something against using this module, you can also easily create your own version:

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

For arrays, dojo.forEach () already exists in the base library.

+9
source

Source: https://habr.com/ru/post/898774/


All Articles