0


Using an angular expression, I tried to show if a scope variable is an array.
I tried to use variable.constructor === Array for checking the same, but in expression always showing false as result.

When I used a scope function to return variable.constructor === Array, I got the correct result.

Can someone tell, why expression behaves in this manner. Also, how same check can be done as an inline expression statement.

Checkout the Plunker demonstrating same problem, here
Thanks.

devesh-ahuja
  • 963
  • 1
  • 8
  • 24

3 Answers3

3

There is no variable named Array in your scope, so the expression variable.constructor === Array is basically returning the result of variable.constructor === undefined.

That's why it's always false.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
1

There is nothing known as Array, so you are essentially comparing a value to undefined.

To test that a value is an array, you can use the in-built Angular function called isArray().

See the answer here for more information: Angular expression to check if model is array or object

Community
  • 1
  • 1
Sarhanis
  • 1,577
  • 1
  • 12
  • 19
1

You'll have to use Array.isArray(variable) in JS.

Further info on MDN

phuzi
  • 12,078
  • 3
  • 26
  • 50