Gus Gus - 25 days ago 11
Javascript Question

How to distinguish an array from an array of arrays

Is it possible to distinguish an array from an array of arrays or array of objects in jQuery?

var a = [1,2,3];
var a2 = [[12,'Smith',1],[13,'Jones',2]];
var a3 = [{val:'12', des:'Smith', num:1}];

//a = array
//a2 and a3 = multidimensional array


How can I do this?
Thanks

Answer

Since you're using jQuery, you can use:

$.isArray(a[0]);

Here's the documentation: http://api.jquery.com/jquery.isarray/

This is definitely not the only way to find out. You could do this in pure JS too, using:

Array.isArray(v[0]);