typescript - 用于自定义数组的 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;
}
我在这里做错了什么?
谢谢,萨杰什
解决方案
尝试这个。
const x = validationErrors.filter(ve => ve.code === (101 as number))[0];
推荐阅读
- sql-server - 如果任何级别的子级或子级的子级没有实用值,则删除一个元素
- node.js - 为什么变量未定义?- 节点 - ElectronJs
- python - 为什么下面的代码返回 Null 而不是“0123456789”
- c - 你如何在C中打印版权符号?
- c - 使用 char 指针打印整数变量的值
- c# - IBM.Data.DB2.Core 在 azure 函数应用程序中引发异常
- python - 如何从文件 1 和文件 2 打印行,其中文件 1 中的第 9 列小于文件 2 中的第 4 列
- node.js - 在 NextJS 中使用动态导入时如何访问 ReactQuill Ref?
- python - Python:如何从 CSV 文件中读取键值对?
- ios - 如何在 Swift 中处理异步 http 请求