xxxxxxxxxx
var extensions = ["image/jpeg","image/png","image/gif"];
if(extensions.indexOf("myfiletype") === -1){
alert("Image must be .png, .jpg or .gif");
}
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
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
const nums = [ 1, 3, 5, 7];
console.log(nums.includes(3));
// true
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
array.includes('element that need to be checked') //returns true or false
xxxxxxxxxx
let isInArray = arr.includes(valueToFind[, fromIndex])
// arr - array we're inspecting
// valueToFind - value we're looking for
// fromIndex - index from which the seach will start (defaults to 0 if left out)
// isInArray - boolean value which tells us if arr contains valueToFind
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;
};