Skip to content

Instantly share code, notes, and snippets.

@everson
Created July 17, 2012 06:21
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 everson/3127567 to your computer and use it in GitHub Desktop.
Save everson/3127567 to your computer and use it in GitHub Desktop.
Adding map operation to javascript Array
// returns a new array where every object is the result of the passed function applied to corresponding element in the original array
Array.prototype.map = function(fn){
var nArr=[];
for(i=0; i<this.length; i++){
nArr[i] = fn(this[i])
};
return nArr
}
// to use it, define a function taking one argument and processing it:
var plusOne = function(i){return i+1}
// applying it to some Array
var otherArr = [3,5,6,7].map(plusOne); // returns: [4, 6, 7, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment