0
Q:

js check if array

if(Array.isArray(myVarToTest)) {
	// myVatToTest is an array
} else {
	// myVarToTest is not an array
}
0
Array.isArray([1, 2, 3]);	// true
Array.isArray('asdf');		// false
1

function isArray(value) {
    return Object.prototype.toString.call(value) === "[object Array]";
}
1
var colors=["red","green","blue"];

if(Array.isArray(colors)){
    //colors is an array
}
3

 function myFunction() {
  var fruits = ["Banana", "Orange", "Apple", "Mango"];
  var x = document.getElementById("demo");
  x.innerHTML = Array.isArray(fruits);
} 
1
// Check if something is an Array 
// just like you do with "typeof"
Array.isArray([1, 2, 3]);	// true
Array.isArray('asdf');		// false
0
var extensions = ["image/jpeg","image/png","image/gif"];          
if(extensions.indexOf("myfiletype") === -1){
	alert("Image must be .png, .jpg or .gif");
} 
3
var array = [1, 3],
    prizes = [[1, 3], [1, 4]],
    includes = prizes.some(a => array.every((v, i) => v === a[i]));

console.log(includes);
0

New to Communities?

Join the community