Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created July 5, 2023 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/5ef9ada093c70480521ea330273b8f63 to your computer and use it in GitHub Desktop.
Save cferdinandi/5ef9ada093c70480521ea330273b8f63 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>arrayIntersect()</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
/**
* Get the intersecting values between two arrays
* (c) Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Array} arr1 The first array
* @param {Array} arr2 The second array
* @return {Array} The array of overlapping values
*/
function arrayIntersect (arr1, arr2) {
return arr1.filter(function (item) {
return arr2.includes(item);
});
}
let wizards = ['Merlin', 'Gandalf', 'Ursula'];
let magicalFolk = ['Gandalf', 'Radagast', 'Ursula', 'Morgana'];
// returns ['Gandalf', 'Ursula']
let overlap = arrayIntersect(wizards, magicalFolk);
console.log(overlap);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment