首页 > 解决方案 > 用于自定义数组的 Typescript .find

问题描述

我正在尝试使用 .find 方法,但我没有定义。

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;
}

我在这里做错了什么?

谢谢,萨杰什

标签: typescriptangular8

解决方案


尝试这个。

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


推荐阅读