javascript - test if array is multidimensional - Stack Overflow

How would I test to see if a variable includes an multidimensional array? For example:var obj = [1, 2

How would I test to see if a variable includes an multidimensional array? For example:

var obj = [1, 2];

vs

var obj = [[1], [2]];

I have used Array.isArray(obj) to test whether is an array, but I can't figure out how to test the number of arrays.

How would I test to see if a variable includes an multidimensional array? For example:

var obj = [1, 2];

vs

var obj = [[1], [2]];

I have used Array.isArray(obj) to test whether is an array, but I can't figure out how to test the number of arrays.

Share Improve this question edited May 23, 2014 at 18:33 user3180105 asked May 23, 2014 at 17:54 user3180105user3180105 3815 silver badges14 bronze badges 6
  • 2 That's easy, open the console and the second one will generate a syntax error ! – adeneo Commented May 23, 2014 at 17:55
  • if( Object.prototype.toString.call( someVar ) === '[object Array]' ) { alert( 'Array!' ); } – tymeJV Commented May 23, 2014 at 17:57
  • 3 A variable can always only contain a single value, hence it can only contain a single array. Maybe you used the wrong words to describe the problem. If so, please provide more context. – Felix Kling Commented May 23, 2014 at 17:59
  • 1 Do u mean multidimensional arrays? – Comfortably Numb Commented May 23, 2014 at 18:00
  • 1 Do you mean var ratings = [[1], [2]]? If so, you'd have to then check if each value is an array as well as checking if ratings is an array. – forgivenson Commented May 23, 2014 at 18:01
 |  Show 1 more ment

1 Answer 1

Reset to default 7

Assuming you meant

var ratings = [[1], [2]];

as var ratings = [1], [2]; is a syntax error, you could do

ratings.filter(Array.isArray).length

to get the number of arrays inside the wrapping array (2)

FIDDLE

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745311305a4622010.html

相关推荐

  • javascript - test if array is multidimensional - Stack Overflow

    How would I test to see if a variable includes an multidimensional array? For example:var obj = [1, 2

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信