Delete elements in an array based on another array in Javascript

I have just started learning the array methods in javascript. I want to delete elements in an array based on another array. I tried with the below approach but didn't get the expected result.

const array1 = [1, 2, 3, 4, 5, 6, 7];
const array2 = [2, 4, 5];

array2.forEach(a2 => {
  array1.filter(a1 => a1 !== a2)

// actual output: Array [1,2,3,4,5,6,7];
// expected output: Array [1,3,6,7]

Which would be the best array method suited here?

Read more here:

Content Attribution

This content was originally published by Subrato Patnaik at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: