如何使用 Id 数组过滤我的对象列表 - 角度/打字稿



我想用数组过滤以下代码。

MyArray = ['1','2,'3']
this.productlist = listComesFromASubscription.filter((product: Product) => product.productId === this.MyArray;

有人能帮我吗?我想让productlist只包含Array MyArray中Id为的产品。

MyArray = ['1','2,'3']
this.productlist = listComesFromASubscription.filter((product: Product) => this.MyArray.includes(product.productId))

您可以使用Array.includes((。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

this.productlist = listComesFromASubscription.filter((product: Product) => MyArray.includes(product.productId));

您可以使用MyArray.includes((来完成此操作

this.productlist = listComesFromASubscription.filter((product: Product) => this.MyArray.includes(product.productId))

最新更新