sajesh Nambiar
sajesh Nambiar

Reputation: 699

Typescript .find for Custom Array

I am trying to use .find method but I am getting undefined.

const ValidationMsg = '[{        "enum": "RequiredInformationMissing",        "code": 101,        
"title": "Required information not provided",        "message": "The following information is needed 
to complete :" },    {        "enum": "RequiredInformationMissing_Details",        "code": 102,        
"title": "Required information not provided",        "message": "The following information is needed 
to complete the :"    }]';

const validationErrors : Array<ValidationError> = JSON.parse(ValidationMsg);
const x = validationErrors.find(ve => ve.code === (101 as number));

export class ValidationError {    
 code: number;
 message: string;
}

What am i doing wrong here?

Thanks, Sajesh

Upvotes: 0

Views: 353

Answers (2)

sajesh Nambiar
sajesh Nambiar

Reputation: 699

Thanks everyone, as you all pointed out correctly the issue was due to nested Arrays declaration.

Upvotes: 0

Sher Singh
Sher Singh

Reputation: 545

Try this.

const x = validationErrors.filter(ve => ve.code === (101 as number))[0];

Upvotes: 1

Related Questions