I encountered a problem when working on a project, to determine whether an object is an empty object. I found that it can be judged this way, and the code is:
1. Code 1:
var a = {}; if(!a){ console.log(1);} else if(a == null) { console.log(2);} else { console.log(3);}
The result is: 3
2. Code 2:
var b = {}; if(b == {}){ console.log(4);} if(b == '{}') { console.log(5);} if(typeof(b) == 'object') { console.log(6);}
The result is: 6
3. Code 3:
var c = {}; if(JSON.stringify(c) == "{}"){ console.log(7);}
var c = {}; if(JSON.stringify(c) == "{}"){ console.log(7);}
The result is: 7
So you can use the method of code 3 to determine whether the object is an empty object{};
If the object is not empty, and you know that when the object is not empty, a certain attribute (such as {id:111}) must exist, you can judge like this:
4. Code 4:
var d = {}; var e = {id:111}; if(d.id){ console.log(8);} if(e.id){ console.log(9);}
The result is: 9
Summary: Obviously the judgment method of code 3 is relatively "strong", but the efficiency is obviously not as good as the judgment method of code 4
The above is the simple method of JavaScript to determine whether an object {} is an empty object brought by the editor. I hope everyone will support the PHP Chinese website~
More JavaScript to determine whether an object { }For related articles on the simple method of whether it is an empty object, please pay attention to the PHP Chinese website!