如何判断变量是否为数组
JavaScript typeof运算符为数组返回“Object”。ECMAScript 5定义了一种新方法Array.isArray():
let fruits = ["Apple", "Mango", "Banana", "Orange"];
Array.isArray(fruits);
如果对象是由给定的构造函数创建的,那么还可以使用instanceof操作符返回true:
let fruits = ["Apple", "Mango", "Banana", "Orange"];
if (fruits instanceof Array) {
// statements to execute
}
页:
[1]