angular - 在对象数组中搜索值
问题描述
我有一个看起来像这样的对象数组
class ValueError {
value: string;
error?: string;
constructor() {
this.value = null;
this.error = null;
}
}
export class Record {
productId: ValueError;
dpt: ValueError;
storeNumber: ValueError;
email: ValueError;
}
我想在这个数组中搜索产品 id(值)和 storeNumber(值)。这是我到目前为止所尝试的。我不知道如何搜索值
searchBy = '123';
filter() {
const result = JSON.parse(JSON.stringify(this.allRecords));
Object.keys.forEach(key => {
result[key] = result[key].filter(r=> (r.productId.value) && (this.searchBy.includes(r.productId.value)))
})
}
解决方案
下面的方法将返回您过滤的数组。因为你似乎有一个对象数组。因此可以使用普通的过滤器方法返回具有过滤值的新数组。
public filter() {
return result.filter((r) => (r.productId.value) && (this.searchBy.includes(r.productId.value)));
}
推荐阅读
- c# - 如何创建 C# 8.0 控制台应用程序?
- c# - Form Recognizer SDK,使用自定义模型分析,文件流问题
- javascript - 剪辑路径的边缘/IE 问题
- javascript - 如何在 JavaScript If 语句中添加多个 CSS 属性?
- google-sheets - 使用条件格式突出显示一组数据是否匹配另一组数据
- java - Bukkit - 输入后在聊天中显示命令
- c++ - Clang tidy:在表达式末尾添加括号
- reactjs - React Redux thunk - 调度完成后渲染应用程序
- django - Django模型中具有外键的重复值
- python - psycopg2 中的 Python Alter 语句不更新 Peewee