zoukankan      html  css  js  c++  java
  • 如何使用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></span>
    		</p>
    
    		<p>
    			数组nonExistantArray是否为空或存在:
    			<span></span>
    		</p>
    
    		<p>
    			数组fineArray是否为空或存在:
    			<span></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;
    				else
    					output = false;
    
    				document.querySelector('.output-empty').textContent = output;
    
    				if(Array.isArray(nonExistantArray) && nonExistantArray.length)
    					output = true;
    				else
    					output = false;
    
    				document.querySelector('.output-non').textContent = output;
    
    				if(Array.isArray(fineArray) && fineArray.length)
    					output = true;
    				else
    					output = false;
    
    				document.querySelector('.output-ok').textContent = output;
    			}
    		</script>
    	</body>
    
    </html>

    广州设计公司https://www.houdianzi.com 我的007办公资源网站https://www.wode007.com

    方法二:使用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></span>
    		</p>
    
    		<p>
    			数组nonExistantArray是否为空或存在:
    			<span></span>
    		</p>
    
    		<p>
    			数组fineArray是否为空或存在:
    			<span></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>
  • 相关阅读:
    mysql root密码重置
    fetch跨域问题
    HTML5触摸事件(touchstart、touchmove和touchend)
    react-router-dom
    清理网站缓存
    从零开始学java (标识符,关键字,基本数据类型)
    从零开始学java ( 初始java)
    入职一年心得
    guava 函数式编程三兄弟
    java读取各种类型文件
  • 原文地址:https://www.cnblogs.com/qianxiaox/p/13859470.html
Copyright © 2011-2022 走看看