Reputation: 2552
I'm having some troubles while filtering an array of objects based on a set of values:
This is the array to be filtered:
var items: Product[] = ... values
Now I declare an array of the products that I want to select:
var sel: Product[] = ... values
The property on which I have to apply the filter is idProduct, how can I do it?
I need something like this:
var query = items.filter( x => x.idProduct IN (List of idProduct from sel Array)
How can I do it?
Thanks to support
Upvotes: 1
Views: 1463
Reputation: 15313
Based on the other answers but using Array.prototype.includes():
let query = items.filter(x => sel.map(y => y.idProduct).includes(x.idProduct));
Upvotes: 1
Reputation: 249506
You can use some
to find any product in sel
with the same product id
items.filter(i=> sel.some(s=> s.idProduct == i.idProduct));
Upvotes: 2