Just make a little helper function that does what you said, loops through the array and checks each value. An efficient way to do that is with Array.some, as it will return true as soon as it finds a truthy match.
let validSLDs = ['hulu','netflix']
let string1 = 'www.hulu.com/watch/...';
let string2 = 'http://www.netflix.com/watch/...';
let string3 = 'imdb.com/title/....'
const isURLOK = (testString) => validSLDs.some(v => testString.indexOf(v) > -1);
console.log(isURLOK(string1));
console.log(isURLOK(string2));
console.log(isURLOK(string3));
Answer from James on Stack OverflowVideos
Just make a little helper function that does what you said, loops through the array and checks each value. An efficient way to do that is with Array.some, as it will return true as soon as it finds a truthy match.
let validSLDs = ['hulu','netflix']
let string1 = 'www.hulu.com/watch/...';
let string2 = 'http://www.netflix.com/watch/...';
let string3 = 'imdb.com/title/....'
const isURLOK = (testString) => validSLDs.some(v => testString.indexOf(v) > -1);
console.log(isURLOK(string1));
console.log(isURLOK(string2));
console.log(isURLOK(string3));
let string1 = 'www.hulu.com/watch/...';
let string2 = 'http://www.netflix.com/watch/...';
let string3 = 'imdb.com/title/....'
let unverfiedSLDs = [string1, string2, string3]
let validSLDs = ['hulu', 'netflix', 'imdb'];
let allSLDsAreValid = unverifiedSLDs.every(s => controlSLDs.includes(s))
allSLDsareValid is a boolean, it evaluates to true if all strings are valid, and false if there is at least one invalid string.
If instead you want to keep track of which SLDs are valid and which are not, try using an object:
let validatedStrings = {}
unverifiedSLDs.forEach(s => {
if (validSLDs.includes(s)) {
SLDs[s] = true;
}
})
Then when you need to access the SLD's validity, you can check for the key existence in validatedStrings:
validatedStrings[string1] = true
validatedStrings[string2] = true
validatedStrings[string3] = true
validatedStrings['not-valid'] = undefined