Skip to content

Instantly share code, notes, and snippets.

@ophentis
Created December 10, 2013 13:44
Show Gist options
  • Save ophentis/7890752 to your computer and use it in GitHub Desktop.
Save ophentis/7890752 to your computer and use it in GitHub Desktop.
array dimension for javascript
//
// [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