判断对象是否为空对象的方式

//判断对象为空对象
// 1. Object.keys()
let obj = {

}
function getObjLength(){
    console.log(Object.keys(obj).length === 0) //true
}                                                                 
getObjLength()

// 2.Object.entries()
function isEmptyObj(){
    console.log(Object.entries(obj).length=== 0)//true
}
isEmptyObj()

//3.for..in
function isEmpty(){
    for (let i in obj){
        if (obj.hasOwnProperty(i)) {
            return false;
        }
    }

    return true;
}
console.log(isEmpty())//true

//4.JSON.stringify
console.log(JSON.stringify(obj)=='{}') //true

 

 

 

posted on 2024-03-18 10:14  萬事順意  阅读(11)  评论(0编辑  收藏  举报