-
-
Save cferdinandi/5ef9ada093c70480521ea330273b8f63 to your computer and use it in GitHub Desktop.
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
<!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