1

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

2 Answers 2

2

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)); 
Sign up to request clarification or add additional context in comments.

2 Comments

where does sel come from? its a standard feature ?
My understanding was you already had a an array sel of products, and you wanted to filter another array using it
1

Based on the other answers but using Array.prototype.includes():

let query = items.filter(x => sel.map(y => y.idProduct).includes(x.idProduct));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.