Created
December 10, 2013 13:44
-
-
Save ophentis/7890752 to your computer and use it in GitHub Desktop.
array dimension for javascript
This file contains 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
// | |
// [1].dim() == 1 | |
// [1,2].dim() == 1 | |
// [[1]].dim() == 2 | |
// [[1,2,3],4,[5,6,7],[8,9,0]].dim() == 2 | |
// [[1,[2]],3,[[4,5],6,[7,8]]].dim() == 3 | |
// | |
Array.prototype.dim = function dim() { | |
return this.reduce(function(a,b) { | |
return ( ((a.dim===dim&&a.dim()) || 0) ) + ( (b.dim===dim&&b.dim()) || 0 ) + 1; | |
},0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment