如何检查对象是否为数组?

时间:2022-06-23 07:01:08

What is the correct and best way to check the object is an array or not ?

检查对象的正确和最佳方法是数组与否?

$scope.studentArray= [{student1},{student2},{student3}];

5 个解决方案

#1


3  

Please use this http://docs.angularjs.org/api/ng/function/angular.isArray

请使用此http://docs.angularjs.org/api/ng/function/angular.isArray

angular.isArray($scope.studentArray);

#2


2  

I would install Underscore.js and use the isArray function like so:

我会安装Underscore.js并使用isArray函数,如下所示:

_.isArray($scope.studentArray);

#3


0  

What about:

关于什么:

if(Object.prototype.toString.call($scope.studentArray) === '[object Array]') {
  // is array
}

#4


0  

i recommend Lo-Dash it uses underscore plus some nice features

我推荐Lo-Dash它使用下划线加上一些不错的功能

syntax is similar/same as underscore

语法与下划线类似/相同

greetings

问候

#5


0  

Cleanest solution without any libraries would be:

没有任何库的最干净的解决方案是:

var arr = ['val1','val2','val3'];
console.log(arr instanceof Array); // true

var astring = 'test';
console.log(astring instanceof Array); // false;

#1


3  

Please use this http://docs.angularjs.org/api/ng/function/angular.isArray

请使用此http://docs.angularjs.org/api/ng/function/angular.isArray

angular.isArray($scope.studentArray);

#2


2  

I would install Underscore.js and use the isArray function like so:

我会安装Underscore.js并使用isArray函数,如下所示:

_.isArray($scope.studentArray);

#3


0  

What about:

关于什么:

if(Object.prototype.toString.call($scope.studentArray) === '[object Array]') {
  // is array
}

#4


0  

i recommend Lo-Dash it uses underscore plus some nice features

我推荐Lo-Dash它使用下划线加上一些不错的功能

syntax is similar/same as underscore

语法与下划线类似/相同

greetings

问候

#5


0  

Cleanest solution without any libraries would be:

没有任何库的最干净的解决方案是:

var arr = ['val1','val2','val3'];
console.log(arr instanceof Array); // true

var astring = 'test';
console.log(astring instanceof Array); // false;