[JavaScript] How to remove all elements from an array which are present in another array?


I want to remove all elements in an array with a condition, the condition is if the element present in another array, how can I do this in Javascript?

1 Answer

3 years ago by

Using filter() method along with !array.includes() you can able to remove the elements present in another array.

Below code shows how to remove harshColors from allColors.

let allColors = [ "red", "green", "blue", "orange", "maroon" ];
let harshColors = [ "red", "maroon" ];

console.log(allColors.filter(ele => !(harshColors.includes(ele)))); // Outputs [ 'green', 'blue', 'orange' ]

Run here https://onecompiler.com/javascript/3xnkhjz29

3 years ago by Meera