Open bigbigDreamer opened 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))
如何判断一个对象是否为空?
方案一
方案二