Home > Enterprise >  filter function - how to check if the id of an item matches any id in a list
filter function - how to check if the id of an item matches any id in a list

Time:10-01

I have a list of 'items' and each 'item' has an 'id'. I also have a list of 'ids'. I would like to filter down my list of 'items' so that each item in my list must also match any 'id' in my list of 'ids'.

I basically want to filter out all items that do not have a corresponding id in my list of ids.

This is what I was trying, I am not sure how to check each id in my list of ids in this filter function.

items = items.filter(item => item.id !== itemIds)

CodePudding user response:

Array.prototype.includes

items = items.filter(item => !itemIds.includes(item.id))
  • Related