1、【最标准】判断数组写法
const arr='1,2,3,4,5,5,3,4,5,3,6';
function isArray(arr) {
return Object.prototype.toString.call(arr) == '[object Array]';
}
console.log(isArray(arr)) // true、数组;false、不是数组
2、【ES6最简单】Array.isArray() 用于确定传递的值是否是一个 Array。
const arr = [1,2,3,4,5,5,3,4,5,3,6];
console.log(Array.isArray(arr)) // true 是数组;false 不是数组
3、【补充】instanceof
、constructor
当检测Array实例时, Array.isArray
优于 instanceof,因为Array.isArray能检测iframes
.
const arr=[1,2,3,4,5,5,3,4,5,3,6];
console.log(arr.constructor) // true 是数组;false 不是数组
console.log(arr instanceof Array) // true 是数组;false 不是数组
参考文章:MDN文档