首页 > 解决方案 > 获取方法它在其他函数中工作,但在另一个我看不到获取方法 TS

问题描述

我正在更改 TS 文件中的一些数据,并且我创建了两个变量,第一个是它正在工作的 get 方法,但第二个不是,我不再需要第一个,因为我正在更改完整的代码,我该如何使用换一种方式。我正在尝试在 TestProdcutIds 中使用 get 方法这是我的代码。

这里的 get 方法可以正常工作,并且一切正常。

getCustomersPurchasedProduct(): Customer[] {
    return this.customers.toArray().filter(customer => customer.purchasedProductIds.get(this.selectedProduct.id));
  }

在这里我不能放我刚刚看到的获取数据

.concat
.copyWithin
.entries.
.values
.filter
.reduce and some other methods.


CustomersPurchasedProduct(): Customer[] {
   return  this.customers.toArray().filter(customer => customer.TestProductIds.(this.selectedProduct.id));
  }

这是客户界面中的内容。

export interface Customer {
  id: string;
  name: string;
  purchasedProductIds: Map<string, number>;
  TestProductIds: string[];

标签: javascriptangulartypescript

解决方案


由于您检查数组中是否包含特定值,Array.prototype.indexOf()因此应该这样做:

CustomersPurchasedProduct(): Customer[] {
  return this.customers.toArray().filter(customer =>
      customer.TestProductIds.indexOf(this.selectedProduct.id) >= 0);
}

或者您可以使用Array.prototype.includes()(取决于您所针对的 ECMAScript 版本):

CustomersPurchasedProduct(): Customer[] {
  return this.customers.toArray().filter(customer =>
      customer.TestProductIds.includes(this.selectedProduct.id));
}

推荐阅读