1.判断对象非空
let obj1 = {}
let obj2 = {a:1}
function empty(obj){
for (let key in obj){
return false; //非空
}
return true; //为空
}
console.log(empty(obj1)) //true为空
console.log(empty(obj2)) //false非空
2.去掉null的键值对
let obj={
'a':'a',
'b':null,
'c':'c'
}
var removePropertyOfNull=function(obj){
Object.keys(obj).forEach(item=>{
if(!obj[item]) delete obj[item]
})
return obj;
}
removePropertyOfNull(obj) //obj={'a':'a','b':'b'}