0

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

$scope.studentArray= [{student1},{student2},{student3}];
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85

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,928
  • 1
  • 12
  • 14
0

What about:

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

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

syntax is similar/same as underscore

greetings

nilsK
  • 4,323
  • 2
  • 26
  • 40
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
  • 16,118
  • 3
  • 30
  • 49