当前位置: 首页 > article >正文

js中判断一个变量是数组的方式有哪些

常用的方式有以下4种:

  1. Array.isArray(list);
  2. list instanceof Array;
  3. Object.prototype.toString.call(list) === “[object Array]”
  4. list.constructor === Array

注:list是要判断的变量。

let list = [1,2,3]
// 判断list是否是个数组
if (Array.isArray(list)) {
    console.log("It's an array");
} else {
    console.log("It's not an array");
}
// 再换个方式判断list是否是个数组
if (list instanceof Array) {
    console.log("It's an array");
} else {
    console.log("It's not an array");
}

// 再换个方式判断list是否是个数组
if (Object.prototype.toString.call(list) === "[object Array]") {
    console.log("It's an array");
} else {
    console.log("It's not an array");
}
// 再换个方式判断list是否是个数组
if (list.constructor === Array) {
    console.log("It's an array");
} else {
    console.log("It's not an array");
}


http://www.kler.cn/a/145103.html

相关文章:

  • 392.判断子序列
  • 表格结构标签
  • thinkphp6+swoole使用rabbitMq队列
  • (笔记+作业)书生大模型实战营春节卷王班---L0G2000 Python 基础知识
  • 低代码产品插件功能一览
  • Origami Agents:AI驱动的销售研究工具,助力B2B销售团队高效增长
  • [DFS深度优先搜索]集合里的乘法
  • 如何使用nginx部署静态资源
  • 脸爱云一脸通智慧管理平台未授权访问
  • 【web】Fastapi自动生成接口文档(Swagger、ReDoc )
  • 智安网络|如何最大限度地提高企业网络安全水平
  • GPT、GPT-2、GPT-3论文精读笔记
  • oracle rac环境归档日志清除
  • 数据结构与算法【B树】的Java实现+图解
  • el-table表格排序(需要后端判别),el-table导出功能(向后端发送请求)
  • FlinkCDC实现主数据与各业务系统数据的一致性(瀚高、TIDB)
  • 5 个适用于 Windows 的顶级免费数据恢复软件
  • stm32中断调用流程
  • Python web自动化测试 —— 文件上传
  • ElasticSearch之文件描述符的数量
  • 从二极管到linux服务器
  • Matplotlib网格子图_Python数据分析与可视化
  • C语言每日一题(39)寻找两个正序数组的中位数
  • Failed to initialize NVML: Driver/library version mismatch
  • Nginx高级
  • BART 并行成像重建:ESPIRiT 重建(两组ESPIRiT maps)