Last active
March 3, 2020 20:57
-
-
Save mattbontrager/2a9b71c7110ab61f9ef948208646c85f to your computer and use it in GitHub Desktop.
Find common elements between two arrays.
This file contains 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
Array.prototype.same = function(arr2) { | |
let same = new Set(); | |
let arr1 = this; | |
arr1.forEach(item => !!arr2.includes(item) && same.add(item)); | |
arr2.forEach(item => !!arr1.includes(item) && same.add(item)); | |
return Array.from(same); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment