javascript remove all the common value from array 1

javascript remove all the common value from array

toRemove =[1,2];
myArray = [1,2,3,5];

myArray = myArray.filter( function( el ) {
  return toRemove.indexOf( el ) < 0;
} );

Here is what the above code is Doing:
1. We create an array of elements to remove.
2. We create an array of elements to filter.
3. We use the filter() method to filter out the elements we don't want.
4. We use the indexOf() method to check if the current element is in the array of elements to remove.
5. If the current element is in the array of elements to remove, we return false.
6. If the current element is not in the array of elements to remove, we return true.
7. The filter() method returns a new array with only the elements that return true.

Similar Posts