首页 > 解决方案 > 在对象数组中搜索值

问题描述

我有一个看起来像这样的对象数组

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

标签: angular

解决方案


下面的方法将返回您过滤的数组。因为你似乎有一个对象数组。因此可以使用普通的过滤器方法返回具有过滤值的新数组。

public filter() {
  return result.filter((r) => (r.productId.value) && (this.searchBy.includes(r.productId.value)));
}

推荐阅读