What is the map method in the iterator in dojox?
Iterators
The dojox.collections module contains the Iterator class. The Iterator class can be used to iterate over dojox.collections objects and JavaScript arrays.
What is the map()method?
The map() function will execute a callback function to all the elements in the collection.
Syntax
map(callbackFn, scope);
Parameters
This method takes two arguments:
callbackFn(): This is a function that will be executed for all elements of thedojox.collectionsobjects in the order of the iteration.scope: This reflects thethisscope of the callback function.
Code example
Let's create an array that contains a list of numbers and use the map() method to loop through the array and multiply each element by 2 and print it.
Code explanation
Line 6: We load the
dojolibrary from the CDN server.Line 8: We load the
dojox/collections/ArrayListmodule.Line 9: After the
ArrayListclass is loaded, we create a newArrayListobject with value[1,2,3,4].Line 10: We use the
getIterator()method to get the iterator object for theArrayListobject.Lines 14–16: We use the
map()method to execute a method for each element of theArrayList. Inside the function, we multiply each element ofArrayListby2and print the value.
Free Resources