10

I have two arrays of AJAX (JSON) response:

One dimension:

[["fili","Chif"],["Bart","deme"],["Bomb","Jyui"],["Joiu","FDPi"],["Doen","abcd"],["drog","MAIC"],["Jasi"
,"abcd"],["Jere","Jibi"]]

Three dimensions:

[[["5","#"],["2","N"],["L","7"],["C","8"],["F","W"],["K","T"],["Q","E"],["Z","\/"]],[["B","O"],["$","P"
],["1","Y"],["H","R"],["3","%"],["I","U"],["M","4"],["A","9"]],[["J","X"],["Bye","G"],["D","V"],["Bye"
,"6"]]]

I try to check if an array is multidimensional but does not work:

if (typeof arr[0][0] != "undefined" && arr[0][0].constructor == Array) {
     return true;
} 
FBN
  • 185
  • 1
  • 1
  • 11
  • possible duplicate of [How to check if a multidimensional array item is set in JS?](http://stackoverflow.com/questions/16883473/how-to-check-if-a-multidimensional-array-item-is-set-in-js) – Oliver Gondža Jun 28 '15 at 21:40
  • Never use `==` or `!=` in Javascript. They're totally broken... go for `===` and `!==` instead. – 6502 Jun 28 '15 at 21:57
  • Ok i will use ===, thanks! – FBN Jun 29 '15 at 08:41

5 Answers5

22

You need to check the first element of Array so use

if(arr[0].constructor === Array)

DEMO

alert("[[]] returns " + ([[]].constructor === Array))
bugwheels94
  • 30,681
  • 3
  • 39
  • 60
9

You can also check all elements in the array so I think it would be more right way in 2019

const is2dArray = array =>  array.every(item => Array.isArray(item));
  • You surely mean `some` and not `every` dear sire. Also stating an arbitrary year in the answer is meaningless – vsync Sep 12 '22 at 11:42
  • @vsync since JS aint static typed, checking for multi dimensionality is always going to be dependent upon the type of input that might be provided to the function. Fortunately and mostly, we know the type of input that our function might encounter. Since, the question gives the input so all answers here are correct and checking first element is fastest. Plus, from question it looks like he would want every if he wants and not some otherwise his code might still fail – bugwheels94 Sep 13 '22 at 20:09
  • @bugwheels94 - it's a matter of the definition of what is a multidimensional array. Should all items be an Array or is it enough that one is. Also depends on the situation, and in OP's situation, it seems `some` would work better. – vsync Sep 13 '22 at 20:43
4

If you like my answer, please vote for the person above me, but here is the above answer reconstructed in function format:

function is2dArray(array){
    if(array[0] === undefined){
        return false;
    }else{
        return (array[0].constructor === Array);
    }
}

demo

JohnP2
  • 1,899
  • 19
  • 17
0
if(array[0][0] === undefined){
    return true;
}else{
    return false;
}

this one checks if the Array is a multi or just a normal array.

Shibhe Tepa
  • 102
  • 4
0

Instead of using the constructor property, you may also use instanceof:

var arr = [];
var obj = {};

(arr instanceof Object)
// -> true

(obj instanceof Object)
// -> true

(arr instanceof Array)
// -> true

(obj instanceof Array)
// -> false
Tim
  • 2,805
  • 25
  • 21