Search Flex Components Free

Custom Search

December 10, 2007

IArray - Iterator Array

I wrote this class a while back to simplify looping over array contents. It makes looping over an array easy, much more logical, and less prone to error.&

The IArray class can be found here:

To loop over an IArray's content you simply pass a function or method to the 'iterate' method as follows:

//create an IArray
a = new IArray()

//add some items to the IArray
a[0] = 'Zero'
a[1] = 'One'
a[2] = 'Two'
a[3] = 'Three'
a[4] = 'Four'
a[5] = 'Five'

//create a function
f = function( a , b ){trace( a + ' ' + b )}

//iterate over the IArray
a.iterate( f )

The iterate method will call the passed function with 2 arguments, rowNumber and rowObject allowing your function to do as it pleases.

You can also iterate over the items in reverse:

//iterate over the IArray and set the reverse flag to true (default is false)
a.iterate( f , true )

You can also iterate and modify the scope of passed function:

//iterate over the IArray forward in the scope of _level0
a.iterate( f , false , _level0 )

I find that it removes gobs of, for, and while loops from dependent classes and makes iteration over an array much simpler and predictable. Plus since it works consistantly, your dependent code is less prone to error. All you need is to pass a function to iterate and off it goes.

Related Flex Tutorials