JS:判断一个空对象或空数组

判断一个空对象

1
2
3
4
5
6
7
function isEmptyObject(obj){
if(JSON.stringify(obj) == "{}"){
console.log("是空对象");
}else{
console.log("不是空对象");
}
}

判断一个空数组

1
2
3
4
5
6
7
function isEmptyArray(arr){
if(JSON.stringify(srr) == "[]"){
console.log("是空数组");
}else{
console.log("不是空数组");
}
}
1
2
3
4
5
6
7
function isEmptyArray(arr){
if(arr == false){
console.log("是空数组");
}else{
console.log("不是空数组");
}
}
1
2
3
4
5
6
7
function isEmptyArray(arr){
if(arr.length == 0){
console.log("是空数组");
}else{
console.log("不是空数组");
}
}

拓展:判断数组

1
2
3
4
1. Array.isArray(arr)
2. arr instanceof Array
3. object.prototype.toString.call(arr) === '[object Array]'
4. arr.constructor === Array
-------------本文结束 感谢您的阅读-------------