博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js 判断一个对象是否是数组
阅读量:714 次
发布时间:2019-03-21

本文共 620 字,大约阅读时间需要 2 分钟。

var arr  =  [1,2,3,4,5]

方法一

arr instanceof  Array

方法二

Array.isArray(arr)

方法三

Object.prototype.toString.call(arr) === "[object Array]"

 

console.log(Object.prototype.toString.call(123)) //[object Number]

console.log(Object.prototype.toString.call('123')) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]

 

 

转载地址:http://ystrz.baihongyu.com/

你可能感兴趣的文章