Skip to content

Instantly share code, notes, and snippets.

@dholdren
Created December 15, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dholdren/67f5927c91bb4d09aed0 to your computer and use it in GitHub Desktop.
Save dholdren/67f5927c91bb4d09aed0 to your computer and use it in GitHub Desktop.
map implementations in javascript
Array.prototype.map = function(f) {
res = new Array(this.length);
for (i=0;i < this.length; i++) {
res[i] = f(this[i]);
}
return res;
};
new_a = [1,2,3,4].map(function(ele){ return ele*2; });
console.log("new_a: "+new_a);
Array.prototype.map_in_place = function(f) {
for (i=0;i < this.length; i++) {
this[i] = f(this[i]);
}
return this;
};
a = [1,2,3,4];
console.log("a before map_in_place: "+a);
a.map_in_place(function(ele){ return ele*5; });
a;
console.log("a after map_in_place: "+a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment