8

Im playing around with NG2 and I am looking for the equivalent of angular.isArray.

Yes, I tried to google it but no luck. Im probably thinking about this problem.

The method I try to use in my ng2-app is this:

function periodsFormat(dates, func) {
  if (!angular.isArray(dates)) { return func(dates); }
  return dates.map(func).join('-');
}

Surely it should work if I manage to replace (!angular.isArray... with something NG2-ish. Thank you!

Update:

Thank you both, I ended doing this:

function periodsFormat(dates, func) {
  if (!Array.isArray(dates)) { return func(dates); }
  return dates.map(func).join('-');
}
Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
user2915962
  • 2,691
  • 8
  • 33
  • 60
  • 1
    Do you need an Angular-specific function? or can you just use basic javascript like in the answers here? http://stackoverflow.com/questions/4775722/check-if-object-is-array – Marc Nov 12 '15 at 15:24
  • I probably dont need an angular-specific function. Not sure why I thought so... – user2915962 Nov 12 '15 at 15:26
  • I would have gone with `if (dates instanceof Array) { console.log("it's an array!");}`, but whatever works. – Marc Nov 12 '15 at 15:29

1 Answers1

7

I suspect you can just check the ctor:

if (dates.constructor !== Array) { ...

Since I believe you wouldn't be dealing with possible wrapped objects anymore.

Angular 1 checked for jQuery/jLite array's if I remember correctly which is why there was a special function for checking arrays.

Phill
  • 18,398
  • 7
  • 62
  • 102