0

Given the following class:

export class condition {
    attribute: string;
    operator: string;
    value: (string | any[]);
    uitype: string;
    valueof: string;
    operationTypeAttributeTypeCode: AttributeTypeCode;
    attributeValueHistory: attributeValueHistory[];
}

for the value field, I want to determine if the type is of type any[] and if it is, peform a forEach on the array values

I have tried type casting such as (obj.value as any[]) != undefined or .length > 0 but cant ever seem to test for the type correctly.

Is there a reliable way to do this?

Thank you

1 Answer 1

3

Just use regular Array.isArray check, like here:

export class Condition {
    attribute: string;
    operator: string;
    value: (string | any[]);
    uitype: string;
    valueof: string;
    operationTypeAttributeTypeCode: AttributeTypeCode;
    attributeValueHistory: attributeValueHistory[];
}

const instance = new Condition();

if (Array.isArray(instance.value)) {
    instance.value.forEach(console.log)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.