0

Say I have two arrays:

arr1 = ['One','Two','Four'];

arr2 = ['One','Two','Three','Five'];

What I am trying to do is compare the 2 arrays, and if arr1 contains a value that doesn't exist in arr2, then delete it from arr1

Based on above, I would expect arr1 to look like this:

arr1 = ['One','Two'];

I know there are similar examples online but the answers I'm finding are 5+ years old, using various approaches and I'm just trying to find a quick and effective and "modern" way to achieve this.

ANy pointers. THank you.

Shrimp1986
  • 39
  • 4
  • 1
    Use `arr1.filter()` with a callback function that uses `arr2.includes()` – Barmar Dec 14 '21 at 00:15
  • What you need is simply called the intersection of two arrays. You could use `arr1.filter( (element) => arr2.indexOf(element) !== -1)` – Dorian Naaji Dec 14 '21 at 00:19

0 Answers0