javascript - 获取方法它在其他函数中工作,但在另一个我看不到获取方法 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[];
解决方案
由于您检查数组中是否包含特定值,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));
}
推荐阅读
- lazy-loading - Is there a way to get a border around image with Lazy Load plugin?
- xamarin.ios - 我可以只为特定部分更改标签的色调颜色和字体属性吗?
- javascript - 在 VS Code 菜单栏中添加菜单
- javascript - 超时后如何将按钮恢复到原始状态
- c++ - c++ 代码在 Linux 中的行为与 Windows 不同?
- youtube - YouTube 数据 API v3 未获得正确的订阅人数
- java - 如何使用 JFreeChart 更改 X 标签
- xpages - 从数据库中读取“模板版本是”
- java - 如何将搜索过滤器面板连接到 ArrayList
并在 ExpandableListView 中显示结果? - python - Python3 ImportError:尝试在没有已知父包的情况下进行相对导入