bigbigDreamer / FCC_Record

个人自我提升与拓展
Apache License 2.0
0 stars 0 forks source link

如何判断一个对象是否为空 #11

Open bigbigDreamer opened 4 years ago

bigbigDreamer commented 4 years ago

如何判断一个对象是否为空?

方案一

// 借助Object.keys(obj)获取键数组,判定长度是否为0
let obj = {}
if(!Object.keys(obj)) {
     console.log(“对象为空!”)
}

方案二

const obj = {

}

function isEmpty(obj) {
    function *entires(obj) {
        for(let i of Object.keys(obj)) {
            yield [i,obj[i]]
        }
    }

    console.log(entires(obj))

    for(let i of entires(obj)) {
        return false
    }

    return true
}

console.log(isEmpty(obj))