1. Convert the JSON object to a JSON string and check whether the string is “{}”.
var data = {}; var b = (JSON.stringify(data) == "{}"); alert(b); //trueCopy the code
2. For in
var obj = {}; var b = function() { for(var key in obj) { return false; } return true; } alert(b()); //trueCopy the code
3. Jquery isEmptyObject method
This method is a jquery wrapper for in, and you need to rely on jquery
var data = {}; var b = $.isEmptyObject(data); alert(b); //trueCopy the code
4. Object. GetOwnPropertyNames () method
This method uses the getOwnPropertyNames method of an Object Object. It retrieves the property names of the Object, stores them in an array, and returns an array Object. We can determine whether the Object is empty by judging the length of the array
Note: This method is not compatible with IE8 and is not tested for other browsers
var data = {}; var arr = Object.getOwnPropertyNames(data); alert(arr.length == 0); //trueCopy the code
5. Use the ES6 object.keys () method
Similar to the 4 method, is a new method in ES6. The return value is also an array of property names in the object
var data = {}; var arr = Object.keys(data); alert(arr.length == 0); //trueCopy the code