Created
October 21, 2011 07:39
-
-
Save dakatsuka/1303308 to your computer and use it in GitHub Desktop.
Array::compact
This file contains hidden or 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
| 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-- |
This file contains hidden or 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
| 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