0

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

$scope.studentArray= [{student1},{student2},{student3}];
Amila Iddamalgoda
  • 3,936
  • 11
  • 44
  • 81

5 Answers5

3

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

angular.isArray($scope.studentArray);
WebSmart
  • 46
  • 4
2

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

_.isArray($scope.studentArray);
Tony Zampogna
  • 1,634
  • 1
  • 11
  • 12
0

What about:

if(Object.prototype.toString.call($scope.studentArray) === '[object Array]') {
  // is array
}
ema
  • 5,467
  • 1
  • 25
  • 30
0

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

syntax is similar/same as underscore

greetings

nilsK
  • 4,247
  • 2
  • 24
  • 39
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;
Dieterg
  • 15,611
  • 2
  • 27
  • 46