1

I just wanted to know if anyone knew of a simple function to check if a given array is multidimensional and, if possible, also see how many dimensions it has.

Here is a sample array:

{"name":"Tiradentes","id":"234534gfgdgd4y5hy","areas":{{"name":"Apucarana", "id":"fh4oth98f894nf89h4"},{etc..., etc....}}}

3 Answers 3

2

Quite (I suppose) simple function counting dimensions BOTH in Arrays and array-like Objects (like in your case):

function countDimensions(arr)
{
  if (! (arr && typeof arr === 'object') ) {
    return 0;
  }
  var maxCount = Math.max.apply(null, $.map(arr, function(n) {
    return countDimensions(n);
  }));
  return 1 + (isFinite(maxCount) ? maxCount : 0);
}

jQuery is used, as 0) it's mentioned in tags, 1) naive checking for Array with instanceof Array can lead to tricky results in multiframe apps, 2) $.map is a convenient shortcut for moving through all the array values.

It can be rewritten in plain JS, of course, but it won't be as compact. )

Had to use isFinite check, as protomax-ing an empty array gives -Infinite.

Sign up to request clarification or add additional context in comments.

5 Comments

Just a quick question. I just used it on my array which is 2-dimensional and it returned 1. Does it start at 0?
Can you give an example of your array?
So, I am getting my array from an $.ajax call that is posting to a PHP page and returning an json array. The array is more or less a follows: {"name":"Tiradentes","id":"234534gfgdgd4y5hy",{{"name":"Apucarana", "id":"fh4oth98f894nf89h4"},{etc..., etc....}}}
Ah, as I suspected. What you got from PHP is not an Array: it's an Object. You can still use this function (with the update), but these - Arrays and Objects - are quite different beasts.
Ahhhh... Thanks! I figured it was a json array
0

try with this

var dims = 0;
function getArrayDimensions(arr) {
 if(typeof(arr) != 'array') return false;
 for(var i = 0; i < arr.length; i++) {
   if(typeof(arr[i]) == 'array') dims++;
   dims += getArrayDimensions(arr[i]);
 }
 return dims;
}

2 Comments

Ouch, no! Has anyone who upvoted this read the code or tried it out? Apparently not, it is ridden by mistakes.
Hint: typeof [] => 'object'
0

JavaScript arrays are not really multi-dimensional, they are lists that can consist of different data types. So you only can determine the depth of an array by testing how often your array contains another array:

var isArray = Array.isArray || function(a) {
    return a instanceof Array || Object.prototype.toString.call(a) == "[object Array]";
};

function getDepth(o) {
    for (var i=0; isArray(o); o=o[0])
        i++;
    return i;
}

A more restrictive implementation would count the levels on which all slots of the array contain other arrays, instead of only inspecting the first one. Results would differ for [[], 0] for example.

function getDepth(o) {
    if (!isArray(o))
        return 0;
    return 1 + (o.length && Math.min.apply(null, o.map(getDepth)));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.