15

In Angular 2 how do I check if a property is an array?

I have tried constructor but get a function as below:

function Array() { [native code] }

My Code:

let content: any = {id: '1324234', value:{id:null}};
if(content.value.constructor === 'Array'){
 console.log('It is an array');
} else {
 console.log('Not an array');
}
2
  • 1
    I guess there is nothing to do with angular2 Commented Dec 19, 2016 at 9:01
  • well Angular 2 does not have javascript function .isArray() Commented Dec 19, 2016 at 9:24

1 Answer 1

42
if(content.value instanceof Array){

See also Test for array of string type in TypeScript

Sign up to request clarification or add additional context in comments.

4 Comments

Could you please tell how to check this in a template?
@VikramSingh You can't. You can put this code into a function and call the function from the template (which is usually a bad idea because of performance issues of calling functions from templates). Rather store the result in a variable and bind to this variable from the template.
but i have dynamic object and i have to display key value pairs in html only if the value is not an Array. I am going with method approach. If you have any better idea please tell me.
Prepare a data structure that contains the data and the result of the type test and use this data structure in the template.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.