rmap: Smart reverse iteration

Some time ago I wrote about my extended map function:

The map function I will present is the most useful map function you'll find. Why? Because it can replace most for loops! And replacing for loops will produce much cleaner JavaScript code.

I have also created a rmap (reverse map, which iterates backwards). A loop like this one:

for(var i=this.blocks.length-1; i >= 0; i--) {
   var block = this.blocks[i];

Can be written as:

rmap(this.blocks, function(block, i) {

rmap is a member of AJS - my JavaScript library.

Code · Interesting · JavaScript · Tips 22. Nov 2007
© Amir Salihefendic. Powered by Skeletonz.