DarioN1
DarioN1

Reputation: 2552

Angular2 - Filter Array of object where property is not in values

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

Answers (2)

bugs
bugs

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

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

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

Related Questions