javascript 判断对象属性值为空

var obj ={'id':1,'name':2,code:'3',status:'',other:''}

我想判断这个obj对象是否存在空的属性值。

如果全部有值就执行函数体

如果存在空值就列出空的属性名称

类似:


if(obj全部有属性值){

} else {

status

other

}


慕斯王
浏览 4418回答 3
3回答

拉莫斯之舞

if(!Object.values(obj).some(v=>v=='')){&nbsp; //全部有值}else{&nbsp; return false;}//orif(Object.values(obj).every(v=>!!v)){&nbsp; //全部有值}else{&nbsp; return false;}修改问题后var arr;if((arr = Object.keys(obj).filter(v=>!obj[v])).length<=0){&nbsp; //全部有值}else{&nbsp; console.log(arr);//[status,other]}

繁华开满天机

var flag = true;for(var prop in obj){&nbsp; &nbsp; if(!obj[prop]){&nbsp; &nbsp; &nbsp; &nbsp; flag = false;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}if(flag){&nbsp; &nbsp; ...}else{&nbsp; &nbsp; return false;}

扬帆大鱼

let func = (obj) => {&nbsp; &nbsp; return Object.keys(obj).length == Object.values(obj).filter(val => val != '').length;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript