Vue JS는 객체가 존재하는지, 객체 속성이 비어 있는지, 객체의 지정된 속성이 존재하는지, 객체가 빈 객체인지 여부를 판단합니다.

1,객체가 존재하는지 확인 -------!$.isEmptyObject()

if(!$.isEmptyObject(obj)){
    
    
	console.log('exit obj')
}else{
    
    
	console.log('no exit obj')
}

2,객체 속성이 비어 있는지 확인----Object.keys().length

if(Object.keys(obj).length!=0){
    
    
	console.log('obj not null')
}else{
    
    
	console.log('obj is null')
}

삼,객체 속성이 존재하는지 확인------hasOwnProperty

if(obj.hasOwnProperty('name')){
    
    
	console.log('exit property')
}else{
    
    
	console.log('no exit property')
}

4,객체가 빈 객체인지 확인

4.1 json 객체를 json 문자열로 변환한 후 문자열이 "{}"인지 확인

var data = {
    
    };
var b = (JSON.stringify(data) == "{}");
alert(b); //true

4.2, 순환판정의 경우

var obj = {
    
    };
var b = funciton(){
    
    
	for (var key in obj){
    
    
		return false;
	}
	return true;
}
alert(b); //true

4.3,ES6의 Object.keys(): 반환 값은 객체의 속성 이름으로 구성된 배열입니다.

var data ={
    
    };
var arr = Object.keys(data);
alert(arr.length==0);//true

Supongo que te gusta

Origin blog.csdn.net/qiaoqiaohong/article/details/113939252
Recomendado
Clasificación