最直接可靠的方法是检查数组的 length 属性是否为 0,1. 使用 arr.length === 0 判断数组是否为空,这是 o(1) 操作且准确高效;2. 避免使用 if (arr) 判断,因为空数组是真值(truthy),条件会成立导致误判;3. 在判断前应先用 array.isarray(arr) 确保目标是数组,防止 null、undefined 或普通对象引发错误或误判;4. 编写通用 isempty 函数时需按顺序处理 null 和 undefined、字符串(考虑 trim 后长度)、数组(length === 0)、对象(object.keys(obj).length === 0),其他类型如数字和布尔值默认不为空,该函数应返回 false。

在 JavaScript 里,要检查一个数组是不是空的,最直接、最可靠的方法就是看它的
length
array.length
0
判断 JavaScript 数组是否为空,我个人最推荐也最常用的就是直接访问数组的
length
比如你有一个数组
myArray
let myArray = []; // 这是一个空数组
let filledArray = [1, 2, 3]; // 这是一个有内容的数组
if (myArray.length === 0) {
console.log("myArray 是空的。"); // 会输出
} else {
console.log("myArray 有内容。");
}
if (filledArray.length === 0) {
console.log("filledArray 是空的。");
} else {
console.log("filledArray 有内容。"); // 会输出
}这种方法之所以靠谱,是因为
length
if (arr)
这是一个非常常见的“陷阱”,很多初学者或者不熟悉 JavaScript 类型转换规则的人会在这里栽跟头。简单来说,在 JavaScript 里,一个空数组
[]
这意味着,如果你写:
let emptyArr = [];
if (emptyArr) {
console.log("这个数组被认为是真的。"); // 这一行会被执行!
}你可能会很惊讶,明明
emptyArr
if
false
0
""
null
undefined
NaN
所以,千万不要用
if (arr)
除了上面提到的
if (arr)
一个比较微妙的点是,你得确保你检查的对象它确实是个数组。搞不好你传进来的是个
null
undefined
{}null
undefined
length
isEmpty
例如:
let notAnArray = null;
// console.log(notAnArray.length); // 这一行会报错:TypeError: Cannot read properties of null (reading 'length')
let maybeAnObject = {};
// console.log(maybeAnObject.length); // 这一行会输出 undefined,因为普通对象没有 length 属性所以,在进行
length
Array.isArray()
function checkIfArrayIsEmpty(arr) {
if (Array.isArray(arr) && arr.length === 0) {
return true; // 确实是空数组
}
return false; // 不是数组,或者有内容
}
console.log(checkIfArrayIsEmpty([])); // true
console.log(checkIfArrayIsEmpty([1, 2])); // false
console.log(checkIfArrayIsEmpty(null)); // false (因为 Array.isArray(null) 是 false)
console.log(checkIfArrayIsEmpty({})); // false (因为 Array.isArray({}) 是 false)这样一来,你的判断就更严谨了,不会因为类型不对而导致意料之外的行为。
isEmpty
既然提到了
isEmpty
isEmpty
在我看来,一个通用的
isEmpty
null
undefined
""
" "
length === 0
{}Object.keys(obj).length === 0
0
0
true
false
基于这些考量,我们可以构建一个相对通用的
isEmpty
function isEmpty(value) {
// 1. 处理 null 和 undefined
if (value === null || typeof value === 'undefined') {
return true;
}
// 2. 处理字符串
if (typeof value === 'string') {
return value.trim().length === 0; // 考虑去除首尾空格后的长度
}
// 3. 处理数组
if (Array.isArray(value)) {
return value.length === 0;
}
// 4. 处理对象
// 注意:typeof null 也是 'object',所以要放在 null 和 undefined 之后判断
if (typeof value === 'object') {
// 检查对象是否有自身的可枚举属性
// Object.keys() 返回一个数组,包含对象所有自身可枚举属性的键名
return Object.keys(value).length === 0;
}
// 5. 其他类型(数字、布尔值等)我们认为它们没有“空”的概念
return false;
}
console.log("--- 通用 isEmpty 测试 ---");
console.log("null:", isEmpty(null)); // true
console.log("undefined:", isEmpty(undefined)); // true
console.log("空字符串 '':", isEmpty("")); // true
console.log("只有空格的字符串 ' ':", isEmpty(" ")); // true
console.log("非空字符串 'hello':", isEmpty("hello")); // false
console.log("空数组 []:", isEmpty([])); // true
console.log("有内容的数组 [1, 2]:", isEmpty([1, 2])); // false
console.log("空对象 {}:", isEmpty({})); // true
console.log("有属性的对象 {a: 1}:", isEmpty({a: 1})); // false
console.log("数字 0:", isEmpty(0)); // false
console.log("布尔值 true:", isEmpty(true)); // false这个
isEmpty
null
undefined
typeof null
'object'
typeof value === 'object'
以上就是js 如何用isEmpty检查数组是否为空的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号