如何使用 jQuery 檢查陣列是否為空?
概述
在 jQuery 中,我們可以使用多種方法輕鬆檢查陣列是否為空。常用的方法包括 length 屬性和 JavaScript 中的陣列是物件,因此可以使用 jQuery 別名符號 $.isEmptyObject(objName)。陣列是元素的集合。因此,為了實現我們的目標,我們應該事先了解 jQuery 的語法以及如何操作 HTML。
有必要在 isEmptyObject() 方法中編寫 “$”,因為 “$” 是 jQuery 的主要庫,其中包含各種方法,如 isEmptyObject()、isArray()、isFunction() 等。在所有這些方法中,都會將對物件的引用作為引數傳遞,以檢查特定方法。
範圍
在本文中,我們討論了以下內容:
各種 jQuery 方法,例如 length 和 isEmptyObject(),
作為引數傳遞的物件引用。
在 jQuery 函式內部初始化空陣列。
語法
在此區域中使用的主要語法:
使用 length 屬性
arr.length
使用 isEmptyObject
$.isEmptyObject(object Name)
在 HTML 中編寫 jQuery 函式之前,我們應該在 HTML 的 <head> 標籤中新增 jQuery 的 cdn 連結。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
方法 01:在此方法中,我們將使用 length 屬性來檢查陣列是否為空。
陣列的 length 屬性將檢查陣列的長度並返回一個數字值。陣列的索引從 0 開始,但長度從 1 開始計算,因此陣列的長度是索引的 +1。當陣列不包含任何元素時,它將返回 0,否則它將返回陣列的長度。
演算法
步驟 1 - 在 HTML 的 body 標籤內建立一個 HTML <button>。
步驟 2 - 在 jQuery 語法中傳遞一個選擇器作為按鈕,在選定的標籤上新增 click() 事件方法操作。
步驟 3 - 建立一個空陣列作為 arr[ ]。
步驟 4 - 使用 if-else 條件中的 length 方法檢查陣列[ ] 的長度。
步驟 5 - 如果陣列[ ] 的長度等於零,則它將返回空,否則如果陣列的長度大於零,則它將返回空。
示例
<!DOCTYPE html> <html lang="en"> <head> <title>Check array empty or not using jQuery</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <style> body{ text-align: center; } </style> </head> <body> <p> <strong>Output: </strong> <span id="outputVal" style="padding: 4px;"></span> </p> <button>Check Array</button> <script> $("button").click(() => var arr = []; if(arr.length==0){ document.getElementById("outputVal").innerText="Array is empty"; document.getElementById("outputVal").style.background="tomato"; } else { document.getElementById("outputVal").innerText="Array is not empty"; document.getElementById("outputVal").style.background="lightgreen"; } }) </script> </body> </html>
方法 02:使用 isEmptyObject() 屬性
isEmptyObject() 方法檢查物件的屬性。由於陣列在 JavaScript 中是物件,因此陣列作為引數透過 isEmptyObject(物件名稱) 傳遞。isEmptyObject 的返回型別是布林值,因此它將返回 true 或 false。如果傳遞給它的物件為空,則它將返回 true,否則將返回 false。
演算法
步驟 1 - 在 HTML 的 body 標籤內建立一個 HTML <button>。
步驟 2 - 在 jQuery 語法中傳遞一個選擇器作為按鈕,在選定的標籤上新增 click() 事件方法操作。
步驟 3 - 建立一個空陣列作為 arr[ ]。
步驟 4 - 使用 isEmptyObject() 方法並將陣列作為引數傳遞給它 isEmptyObject(arr),並將返回值儲存在一個變數中。
步驟 5 - 將該變數作為條件傳遞給 if-else。如果它返回 true,則輸出將為陣列為空,否則如果它返回 false,則輸出將為陣列不為空。
示例
<!DOCTYPE html> <html lang="en"> <head> <title>Check array empty or not using jQuery</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"> </script> <style> body{ text-align: center; } </style> </head> <body> <p> <strong>Output: </strong> <span id="outputVal" style="padding: 4px;"></span> </p> <button>Check Array</button> <script> // jQuery starts from here // It is necessary to write the “$” with isEmptyObject() as “$” is the main library of the jQuery which contains this method. $("button").click(() => { var arr = []; var res = $.isEmptyObject(arr); if (res) { document.getElementById("outputVal").innerText = "Array is empty"; document.getElementById("outputVal").style.background = "tomato"; } else { document.getElementById("outputVal").innerText = "Array is not empty"; document.getElementById("outputVal").style.background = "lightgreen"; } }) </script> </body> </html>
以上兩種方法的輸出相同
當陣列為空時
<script> $("button").click(() => { var arr = []; var res = $.isEmptyObject(arr); if (res) { document.getElementById("outputVal").innerText = "Array is empty"; document.getElementById("outputVal").style.background = "tomato"; } else { document.getElementById("outputVal").innerText = "Array is not empty"; document.getElementById("outputVal").style.background = "lightgreen"; } }) </script>

當陣列已填充時
<script> $("button").click(() => { var arr = [1,2,3,4]; var res = $.isEmptyObject(arr); if (res) { document.getElementById("outputVal").innerText = "Array is empty"; document.getElementById("outputVal").style.background = "tomato"; } else { document.getElementById("outputVal").innerText = "Array is not empty"; document.getElementById("outputVal").style.background = "lightgreen"; } }) </script>

結論
jQuery 簡化了 HTML 文件物件模型 (D.O.M.) 中的操作,因為它具有可與陣列一起使用的預製屬性。jQuery 使程式碼編寫更短、更簡單。“isEmptyObject” 儲存物件的引用,從中檢查空值。除了基本的 if-else 語句之外,我們還可以使用三元運算子。
($.isEmptyObject(arr))? alert(“array is empty”) : alert(“array is not empty”);
它提供單行解決方案而不是多行解決方案。