Skip to content

Instantly share code, notes, and snippets.

@ww24
Created June 30, 2012 07:18
Show Gist options
  • Save ww24/3022772 to your computer and use it in GitHub Desktop.
Save ww24/3022772 to your computer and use it in GitHub Desktop.
Array.prototype.depth
// 配列の深さ(次元)を返す
Array.prototype.depth = function () {
function isArray(a) {
return typeof(a) === "object" && (a instanceof Array);
};
var arr = this;
return (function(a, b) {
a = a || arr;
b = b || 0;
if (isArray(a) && a.length) {
b++;
var c = [];
for (var i=0,l=a.length; i<l; i++) {
c[c.length] = arguments.callee(a[i], b);
}
return Math.max.apply(null, c);
}
return b;
})();
};
// Usage
console.log([8,[[9]]].depth());
// -> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment