12Aug/097
A simple map function for plain JavaScript arrays
So this isn't anything new, but sometimes you have to work with plain JavaScript and can't lean on a library. I thought I'd put this out there. This is my map function. There are many like it, but this one is mine.
CODE:
-
Array.prototype.map = function(fn) {
-
var r = [];
-
var l = this.length;
-
for(i=0;i<l;i++)
-
{
-
r.push(fn(this[i]));
-
}
-
return r;
-
};
note bene: Why do I declare the "l" variable at all instead of just checking i against this.length directly? The JavaScript for loop is much faster if you don't perform the length lookup and instead check against a static value.



Pingback: Daily Links for Thursday, August 13th, 2009
Pingback: Dew Drop – August 13, 2009 | Alvin Ashcraft's Morning Dew
Pingback: JavaScript: Not for the faint at heart? | Lazycoder
Pingback: A simple map function for plain JavaScript arrays | Lazycoder