xxxxxxxxxx
array.includes('element that need to be checked') //returns true or false
xxxxxxxxxx
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango");
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
var extensions = ["image/jpeg","image/png","image/gif"];
if(extensions.indexOf("myfiletype") === -1){
alert("Image must be .png, .jpg or .gif");
}
xxxxxxxxxx
var array = [1, 3],
prizes = [[1, 3], [1, 4]],
includes = prizes.some(a => array.every((v, i) => v === a[i]));
console.log(includes);
xxxxxxxxxx
function checkAllEven(arr) {
return arr.every(function(x){
return x % 2 === 0
})
}
//using "every" to check every item in array.
xxxxxxxxxx
var contains = function(needle) {
// Per spec, the way to identify NaN is that it is not equal to itself
var findNaN = needle !== needle;
var indexOf;
if(!findNaN && typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function(needle) {
var i = -1, index = -1;
for(i = 0; i < this.length; i++) {
var item = this[i];
if((findNaN && item !== item) || item === needle) {
index = i;
break;
}
}
return index;
};
}
return indexOf.call(this, needle) > -1;
};
xxxxxxxxxx
console.log(myArray.indexOf('three') > -1);
console.log(myArray.includes('three'));