JavaScript 检查数组是否为空

本文将讨论如何在JavaScript中检查数组是否为空。我们有多种方法可以实现这个目标,下面将介绍其中的一些。

检查数组是否为空的方法:

使用array.isArray()方法和array.length属性。

通过检查数组的类型和长度。

使用JavaScript Array.some()方法。

使用JavaScript toSrting()方法。

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

可以通过Array.isArray()方法来检查数组是否存在并确保它是一个数组。该方法将返回true,如果作为参数传递的对象是一个数组。它还会检查数组是否为undefined或null。

可以通过使用array.length属性来检查数组是否为空。这个属性返回数组中的元素个数。如果个数大于0,返回true。

这两个方法和属性可以与AND(&&)运算符一起使用,以确定数组是否存在且不为空。

语法:

Array.isArray(emptyArray) && emptyArray.length

示例: 此示例展示了上述解释的方法。

function checkArray() {

// Input arrays

let emptyArray = [];

let nonExistantArray = undefined;

let fineArray = [1, 2, 3, 4, 5];

// Checking for type and array length

if (Array.isArray(emptyArray) && emptyArray.length)

output = true;

else output = false;

// Display output

console.log("Output for emptyArray:" + output);

// Checking for type and array length

if (

Array.isArray(nonExistantArray) &&

nonExistantArray.length

)

output = true;

else output = false;

// Display output

console.log("Output for nonExistantArray:" + output);

// Checking for type and array length

if (Array.isArray(fineArray) && fineArray.length)

output = true;

else output = false;

// Display output

console.log("Output for fineArray:" + output);

}

// Function call

checkArray();

输出

Output for emptyArray:false

Output for nonExistantArray:false

Output for fineArray:true

方法2:检查数组的类型和长度

可以通过使用typeof运算符来检查数组是否存在,如果数组的类型是“undefined”,则说明数组存在。还可以检查数组是否为“null”。这两个条件可以验证数组是否存在。

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

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

语法:

typeof emptyArray != "undefined" && emptyArray != null && emptyArray.length != null

&& emptyArray.length > 0

示例:

function checkArray() {

// Array inputs

let emptyArray = [];

let nonExistantArray = undefined;

let fineArray = [1, 2, 3, 4, 5];

// Checking array type and length

if (

typeof emptyArray != "undefined" &&

emptyArray != null &&

emptyArray.length != null &&

emptyArray.length > 0

)

output = true;

else output = false;

// Display output

console.log("Output for emptyArray:" + output);

// Checking array type and length

if (

typeof nonExistantArray != "undefined" &&

nonExistantArray != null &&

nonExistantArray.length != null &&

nonExistantArray.length > 0

)

output = true;

else output = false;

// Display output

console.log("Output for nonExistantArray:" + output);

// Checking array type and length

if (

typeof fineArray != "undefined" &&

fineArray != null &&

fineArray.length != null &&

fineArray.length > 0

)

output = true;

else output = false;

// Display output

console.log("Output for fineArray:" + output);

}

checkArray();

输出

Output for emptyArray:false

Output for nonExistantArray:false

Output for fineArray:true

方法3:使用JavaScript的Array.some()方法

JavaScript的 arr.some() 方法用于检查数组中是否至少有一个元素满足参数方法所检查的条件。

语法:

arr.some(callback(element,index,array),thisArg)

示例: 在这个示例中,我们将使用 JavaScript Array.some() 方法来检查一个空数组。

function checkArray() {

// Array inputs

let emptyArray = [];

let fineArray = [1, 2, 3, 4, 5];

// Checking array

let output = emptyArray.some((element) => true);

// Display output

console.log("Output for emptyArray:" + output);

// Checking array

output = fineArray.some((element) => true);

// Display output

console.log("Output for fineArray:" + output);

}

// Function call

checkArray();

输出结果

Output for emptyArray:false

Output for fineArray:true

方法4:使用JavaScript的toString()方法

JavaScript的 Array toString() 方法返回数组元素的字符串表示形式。

语法:

arr.toString()

示例: 在本文中,我们将使用 JavaScript toSrting() 方法将给定的数组转换为字符串,并将其与空字符串进行比较以输出结果。

function checkArray() {

// Array inputs

let emptyArray = [];

let fineArray = [1, 2, 3, 4, 5];

// Checking array

let output = emptyArray.toString() === "";

// Display output

console.log("Output for emptyArray:" + !output);

// Checking array

output = fineArray.toString() === "";

// Display output

console.log("Output for fineArray:" + !output);

}

// Function call

checkArray();

输出

Output for emptyArray:false

Output for fineArray:true