Skip to content

Instantly share code, notes, and snippets.

@AABoyles
Created September 5, 2014 20:01
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 AABoyles/1add41f62c79ffaf8c7d to your computer and use it in GitHub Desktop.
Save AABoyles/1add41f62c79ffaf8c7d to your computer and use it in GitHub Desktop.
An implementation of R's rep function for Javascript Arrays
Array.prototype.repeat = function(times, each) {
if(!times){return this;}
if(!each){each=1;}
var that = this;
var temp=Array(each*this.length);
this.forEach(function(el, i){
for(j = 0; j < each; j++){
temp[i * each + j] = el;
}
});
ret = temp;
for(i = 1; i < times; i++){
ret = ret.concat(temp);
}
return ret;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment