Created
October 22, 2023 00:43
-
-
Save vasfvitor/003256283418a5e573df7707958c128a to your computer and use it in GitHub Desktop.
Remove all elements contained in another array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; | |
| const toRemove = new Set(['b', 'c', 'g']); | |
| --- method 1 --- | |
| myArray = myArray.filter( ( el ) => !toRemove.includes( el ) ); | |
| --- method 2 --- | |
| const difference = myArray.filter( x => !toRemove.has(x) ); | |
| console.log(difference); // ["a", "d", "e", "f"] | |
| reference: https://stackoverflow.com/questions/19957348/remove-all-elements-contained-in-another-array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment