0

Looking for some advice on my little project.

As an example, I have these two arrays

selectedItems = ['a', 'b', 'c', 'd', 'z']
availableItems = ['a', 'c', 'x', 'z']

What I'd like to achieve is to compare my selectedItems array with my availableItems array. If the items in selectedItems is not found in availableItems, then it will be removed from selectedItems

result:

selectedItems = ['a' 'c', 'z']
availableItems = ['a', 'c', 'x', 'z']

I know I can easily achieve this with a couple of for-loops, however I'd like to know if there is a better approach to do this with Javascript

1 Answers1

0

You can use ES6 filter to do this as well.

selectedItems = ['a', 'b', 'c', 'd', 'z']
availableItems = ['a', 'c', 'z']

const result = selectedItems.filter(item => availableItems.includes(item));

console.log(result)
Tareq
  • 5,283
  • 2
  • 15
  • 18