Skip to content

Instantly share code, notes, and snippets.

@mattbontrager
Last active March 3, 2020 20:57
Show Gist options
  • Save mattbontrager/2a9b71c7110ab61f9ef948208646c85f to your computer and use it in GitHub Desktop.
Save mattbontrager/2a9b71c7110ab61f9ef948208646c85f to your computer and use it in GitHub Desktop.
Find common elements between two arrays.
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