1. Convert the json object into a json string, and then determine whether the string is "{}"var data = {}; var b = (JSON.stringify(data) == "{}"); alert(b);//true 2. for in loop judgmentvar obj = {}; var b = function() { for(var key in obj) { return false; } return true; } alert(b());//true 3. jQuery's isEmptyObject method This method is a jQuery encapsulation of the 2 method (for in), and it needs to rely on jQuery when used. var data = {}; var b = $.isEmptyObject(data); alert(b);//true 4.Object.getOwnPropertyNames() method This method uses the getOwnPropertyNames method of the Object object to obtain the property names in the object, store them in an array, and return the array object. We can determine whether this object is empty by judging the length of the array. Note: This method is not compatible with IE8 and has not been tested on other browsers. var data = {}; var arr = Object.getOwnPropertyNames(data); alert(arr.length == 0); //true 5. Use ES6's Object.keys() method Similar to method 4, it is a new method of ES6, and the return value is also an array of attribute names in the object var data = {}; var arr = Object.keys(data); alert(arr.length == 0); //true This concludes this article about 5 ways to use JS to determine whether an object is empty. For more information about how to use JS to determine whether an object is empty, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed tutorial on installing MySQL 5.7.19 decompressed version on Windows Server 2016
>>: Docker containers communicate directly through routing to achieve network communication
Note: Other machines (IP) cannot connect to the M...
The previous article introduced how Vue can reali...
This article example shares the specific code of ...
1. Problem During development, when inserting dat...
1 Background Recently, I have been studying how t...
The editor recently wanted to get started with th...
Table of contents 1 Use of v-if and v-show 2. Dif...
Three ways to configure Nginx The first method di...
/******************** * Application of linked lis...
Note: sg11 Our company only supports self-install...
As the number of visits increases, the pressure o...
The Raspberry Pi model is 4b, 1G RAM. The system ...
Introduction Animation allows you to easily imple...
Copy code The code is as follows: html, address, ...
In new projects, axios can prevent duplicate subm...