JavaScript如何检查数组是否为空?下面本篇文章就来给大家介绍一下使用使用JavaScript检查数组是否为空的方法,希望对大家有所帮助。

方法一:使用Array.isArray()方法和array.length属性

可以通过array.isarray()方法检查该数组是否确实是一个数组。如果作为参数传递的对象是数组,则此方法返回true。它还检查数组是否为“undefined”或为“null”。

使用array.length属性检查数组是否为空;此属性返回数组中的元素数量。如果这个数大于0,它的值为true。

数组的isArray()方法和length属性可与(&&)操作符一起使用,以确定数组是否存在且是否为空。

语法:

Array.isArray(emptyArray) && emptyArray.length

例:

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>检查数组是否为空或存在</title></head><body><b>检查数组是否为空或存在</b><p>emptyArray = []</p><p>nonExistantArray = undefined</p><p>fineArray = [1, 2, 3, 4, 5]</p><p>单击按钮,检查数组是否存在且不为空</p><button onclick="checkArray()">检查数组</button><p>数组emptyArray是否为空或存在:<span class="output-empty"></span></p><p>数组nonExistantArray是否为空或存在:<span class="output-non"></span></p><p>数组fineArray是否为空或存在:<span class="output-ok"></span></p><script type="text/javascript">function checkArray() {let emptyArray = [];let nonExistantArray = undefined;let fineArray = [1, 2, 3, 4, 5];if(Array.isArray(emptyArray) && emptyArray.length)output = true;elseoutput = false;document.querySelector('.output-empty').textContent = output;if(Array.isArray(nonExistantArray) && nonExistantArray.length)output = true;elseoutput = false;document.querySelector('.output-non').textContent = output;if(Array.isArray(fineArray) && fineArray.length)output = true;elseoutput = false;document.querySelector('.output-ok').textContent = output;}</script></body></html>

效果图:

方法二:使用typeof运算符和array.length

通过使用typeof运算符检查数组的类型是否为“undefined”,数组是否为'null',来检查数组是否存在。

通过使用array.length属性,可以检查数组是否为空;通过检查返回的长度是否大于0,可以确保数组不为空。

然后,可以将这些属性与(&&)运算符一起使用,以确定数组是否存在且不为空。

例:

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>检查数组是否为空或存在</title></head><body><b>检查数组是否为空或存在</b><p>emptyArray = []</p><p>nonExistantArray = undefined</p><p>fineArray = [1, 2, 3, 4, 5]</p><p>单击按钮,检查数组是否存在且不为空</p><button onclick="checkArray()">检查数组</button><p>数组emptyArray是否为空或存在:<span class="output-empty"></span></p><p>数组nonExistantArray是否为空或存在:<span class="output-non"></span></p><p>数组fineArray是否为空或存在:<span class="output-ok"></span></p><script type="text/javascript">function checkArray() { let emptyArray = []; let nonExistantArray = undefined; let fineArray = [1, 2, 3, 4, 5]; if (typeof emptyArray != "undefined"  && emptyArray != null  && emptyArray.length != null  && emptyArray.length > 0) output = true; else output = false; document.querySelector('.output-empty').textContent = output; if (typeof nonExistantArray != "undefined"  && nonExistantArray != null  && nonExistantArray.length != null  && nonExistantArray.length > 0) output = true; else output = false; document.querySelector('.output-non').textContent = output; if (typeof fineArray != "undefined"  && fineArray != null  && fineArray.length != null  && fineArray.length > 0) output = true; else output = false; document.querySelector('.output-ok').textContent = output; } </script></body></html>

效果图:

更多web前端知识,请查阅 HTML中文网 !!

使用JavaScript检查数组是否为空相关推荐

  1. 如何使用JavaScript检查输入是否为空

    by Zell Liew 由Zell Liew 如何使用JavaScript检查输入是否为空 (How to check if an input is empty with JavaScript) L ...

  2. php 检查数组为空_检查数组是否为空在PHP中

    php 检查数组为空 Given an array and we have to check if array is an empty or not using PHP. 给定一个数组,我们必须检查数 ...

  3. javascript判断数组是否为空

    方法如下: var daily= json.daily; if(typeof daily.increase_num !=='undefined' && daily.increase_n ...

  4. JavaScript检查数组中是否有重复值

    参考文章:https://www.jianshu.com/p/2cbe951b2997 function repeatnum(arr){if((new Set(arr)).size != arr.le ...

  5. 2种检查JavaScript数组是否为空的方法

    方法一:使用Array.isArray()方法和array.length属性 可以通过array.isarray()方法检查该数组是否确实是一个数组.如果作为参数传递的对象是数组,则此方法返回true ...

  6. html 数组为空 报错,javascript怎么判断数组是否为空?

    javascript判断数组是否为空的方法:通过数组length属性返回数组中元素数目,若为0说明数组为空,若不为0说明数组不为空. 判断数组是否为空实现代码:let arr = [];if (arr ...

  7. 如何检查数组是否包含JavaScript中的对象?

    In this article, we will look at various methods to check if an array includes an object in JavaScri ...

  8. JavaScript 中检查数组是否包含值的 5 种方法

    在 JavaScript 中,有多种方法可以检查数组是否包含项目.您始终可以使用for 循环或Array.indexOf()方法,但 ES6 添加了许多更有用的方法来搜索数组并轻松找到您要查找的内容. ...

  9. JavaScript - 移除数组中的空字符串元素

    移除数组中的空字符串元素 使用 filter 方法对数组进行拷贝,删除空字符串元素,保留其他元素(第 22 ~ 24 行): <!DOCTYPE html> <html>< ...

最新文章

  1. javascript的特点
  2. Vue教程3【使用Vue脚手架】render ref props minin scoped $emit $bus 消息订阅发布 动画
  3. 我在互联网大厂,和同事谈恋爱
  4. Python 框架篇
  5. 2011/5/18工作笔记
  6. 浅析Entity Framework Core中的并发处理
  7. [mybatis]映射文件_select_resultMap_discriminator鉴别器
  8. jsp分割字符串并遍历
  9. 线性代数 —— 线性递推关系
  10. 已激活的windowns、office查看用的密钥
  11. 图像算法九:【图像特征提取】特征降维、PCA人脸特征抽取、局部二进制
  12. 机器学习与计算机视觉(darknet编译)
  13. Math.net,.net上的科学计算利器
  14. rman异机恢复数据库
  15. Arduino的详细介绍(基于Mega2560)(分文)——pinMode,digitalWrite/digitalRead()
  16. 南京java程序员工资_2019年一二线城市java程序员工资大调查
  17. Java多态实例主人和狗狗企鹅玩游戏
  18. 尚学堂1811期python视频_尚学堂1811期Java+架构全套视频教程 下载
  19. Reflex仓库管理系统(WMS)简介
  20. 策略路由(本地策略和接口策略)

热门文章

  1. 王者客服信息服务器,王者荣耀客服反馈在哪?客服反馈位置详解介绍
  2. 计算机里有请将磁盘插入,请将磁盘插入驱动器怎么解决 请将磁盘插入驱动器解决方案...
  3. 计算机界面没磁盘驱动器,win10弹出驱动器中没有磁盘请在驱动器怎么办
  4. 银行业国产数据库现状
  5. 车间数字孪生解决方案(三)
  6. 2022年11月软考领证通知
  7. 大型软件设计需求文档——多媒体播放器
  8. 【uiautomation】微信群发消息,获取群通讯录名单
  9. Electron 从入门到实践07之实战 相机应用
  10. 李抗強:類自然數8階完美,類自然數8階富蘭克林