3种判断方法:1、使用“Array.isArray(数组对象)”语句来判断,如果是数组则返回true。2、使用“数组对象.constructor===Array”语句来判断。3、使用“数组对象 instanceof Array”语句来判断。
本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。
es6判断是否是数组的方法:
方法1:使用isArray()方法
isArray() 方法用于判断一个对象是否为数组。
如果对象是数组返回 true,否则返回 false。
var fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(Array.isArray(fruits)); if(Array.isArray(fruits)){ console.log("是数组"); }else{ console.log("不是数组"); }
方法2:利用constructor 属性
利用数组对象.constructor === Array 语句,如果是数组返回 true,否则返回 false。
var fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.constructor === Array); if(fruits.constructor === Array){ console.log("是数组"); }else{ console.log("不是数组"); }
方法3:利用instanceof运算符
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上
var fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits instanceof Array); if(fruits instanceof Array){ console.log("是数组"); }else{ console.log("不是数组"); }
【相关推荐:javascript视频教程、web前端】
以上就是es6怎么判断是否是数组的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号