Skip to content

Instantly share code, notes, and snippets.

@dakatsuka
Created October 21, 2011 07:39
Show Gist options
  • Save dakatsuka/1303308 to your computer and use it in GitHub Desktop.
Save dakatsuka/1303308 to your computer and use it in GitHub Desktop.
Array::compact
Array::compact = ->
i = @length - 1
while i >= 0
@splice(i, 1) if @[i] is null or @[i] is undefined
return this if i == 0
i--
Array.prototype.compact = function() {
var i = this.length - 1;
while (i >= 0) {
if (this[i] == null || this[i] == void 0) {
this.splice(i, 1);
}
if (i == 0) {
return this;
}
i--;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment