Last active
August 12, 2022 19:52
-
-
Save chrisribe/a22fb36f4e306ac941eb667b8b6e133c to your computer and use it in GitHub Desktop.
Array contains partial and exact match
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
var allowedURLs = [ | |
"https://www.google.com", | |
"https://www.youtube.com/watch?v=mp5WJwT7HW0", | |
"https://www.youtube.com" | |
]; | |
function arrayContains(targetArray, stringTofind, exactMatch) { | |
var matches = targetArray.filter(function (aItem) { | |
if (exactMatch) | |
return (stringTofind === aItem); | |
else | |
return (stringTofind.indexOf(aItem) != -1); | |
}); | |
return matches; | |
} | |
var matches = arrayContains(allowedURLs, "https://www.youtube.com/watch?v=mp5WJwT7HW0"); | |
if(matches.length > 0){ | |
console.log('found match!') | |
} else{ | |
console.log('no') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment