Skip to content

Instantly share code, notes, and snippets.

@straydogstudio
Created February 27, 2013 18:10
Show Gist options
  • Save straydogstudio/5050104 to your computer and use it in GitHub Desktop.
Save straydogstudio/5050104 to your computer and use it in GitHub Desktop.
Useful Javascript Array prototypes
Array.prototype.unique = function() {
var o = {}, i, l = this.length, r = [];
for(i=0; i<l;i+=1) o[this[i]] = this[i];
for(i in o) r.push(o[i]);
return r;
};
Array.prototype.values_at = function() {
var o = [], i, l = arguments.length;
for(i=0; i<l;i+=1) o.push( this[arguments[i]] );
return o;
};
Array.prototype.flatten = function() {
b = Array.prototype.concat.apply([], this);
if (b.length != this.length) b = b.flatten();
return b;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment