Skip to content

Instantly share code, notes, and snippets.

@tincho
Created December 29, 2016 15:41
Show Gist options
  • Save tincho/3ac185722a872465a8e1264ed8cda996 to your computer and use it in GitHub Desktop.
Save tincho/3ac185722a872465a8e1264ed8cda996 to your computer and use it in GitHub Desktop.
Array.prefixEach preppends string to every element in array
/**
* 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