xxxxxxxxxx
const found = arrayOfStrings.find(v => str.includes(v));
xxxxxxxxxx
wordsArray = ['hello', 'to', 'nice', 'day']
yourString = 'Hello. Today is a nice day'.toLowerCase()
result = wordsArray.every(w => yourString.includes(w))
console.log('result:', result)
xxxxxxxxxx
const colors = ['red', 'green', 'blue'];
const result = colors.includes('red');
console.log(result); // true
xxxxxxxxxx
var arr = ["name", "id"];
if (arr.indexOf("name") > -1) console.log('yeah');
else console.log('nah');
xxxxxxxxxx
function checkInput(input, words) {
return words.some(word => input.toLowerCase().includes(word.toLowerCase()));
}
console.log(checkInput('"Definitely," he said in a matter-of-fact tone.',
["matter", "definitely"]));
xxxxxxxxxx
Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE:
console.log(['joe', 'jane', 'mary'].includes('jane')); //true
xxxxxxxxxx
Check array contains element
String[] fruits = new String[] { "banana", "guava", "apple", "cheeku" };
Arrays.asList(fruits).contains("apple"); // true
Arrays.asList(fruits).indexOf("apple"); // 2
Arrays.asList(fruits).contains("lion"); // false
Arrays.asList(fruits).indexOf("lion"); // -1
xxxxxxxxxx
var extensions = ["image/jpeg","image/png","image/gif"];
if(extensions.indexOf("myfiletype") === -1){
alert("Image must be .png, .jpg or .gif");
}
xxxxxxxxxx
const nums = [ 1, 3, 5, 7];
console.log(nums.includes(3));
// true