Created
December 29, 2016 15:41
-
-
Save tincho/3ac185722a872465a8e1264ed8cda996 to your computer and use it in GitHub Desktop.
Array.prefixEach preppends string to every element in 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
/** | |
* Prefixes each array element with given | |
* @param {String} prefix | |
* @usage e.g var pfixed = ["one", "two", "three"].prefixEach("number: "); | |
*/ | |
Array.prototype.prefixEach = function(prefix) { | |
return this.map(_ary(String.prototype.concat.bind(prefix))); | |
}; | |
function _ary(fn, arity) { | |
arity = arity || 1; | |
return function() { | |
var args = Array.prototype.slice.call(arguments, 0, arity); | |
return fn.apply(null, args); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment