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
The fundamental issue you are not understanding is that an Array cannot have strings as indexes. The syntax you are using is an alternate way of defining properties on to an object. All the previous suggestions people are giving you is probably confusing you more. Keep it simple. Arrays have number indexes.
// this is adding values to an array
var x = [];
x[0] = 'one';
x[1] = 'two';
console.log(x[0]); // outputs 'one'
// this is adding a property to an object
var y = {};
y['width'] = 20;
y['height'] = 40;
console.log(y['width']); // outputs 20
console.log(y.height); // outputs 40
// this is adding a property to our previous array
// (because Arrays are objects too in JavaScript)
x['name'] = 'My Array';
console.log(x.name); // outputs 'My Array'
x.indexOf('My Array'); // returns -1 because 'My Array' is not stored in the array
x.indexOf('two'); // returns 1 because that's the index of 'two' in the array
x = [];
x['a'] = 0;
x['b'] = 1;
var valueIndex = Object.keys(x).map(function(prop){
return x[prop]
}).indexOf(1);
Object.keys(x)[valueIndex] //b
Unless it's really going to be in that order just do
Object.keys(x)[1]; //b