Created
July 17, 2012 06:21
-
-
Save everson/3127567 to your computer and use it in GitHub Desktop.
Adding map operation to javascript Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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