5 ways to determine whether an object is an empty object in JS

5 ways to determine whether an object is an empty object in JS

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 judgment

var 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:
  • JavaScript Simple Method to Determine if an Object {} is an Empty Object
  • js determines the instance of an empty object (super simple)
  • A review of several methods for judging empty objects in JS
  • Summary of several practical methods for JS to determine whether an object is an empty object

<<:  Detailed tutorial on installing MySQL 5.7.19 decompressed version on Windows Server 2016

>>:  Docker containers communicate directly through routing to achieve network communication

Recommend

VUE+SpringBoot implements paging function

This article mainly introduces how to implement a...

MySQL 8.0.12 Quick Installation Tutorial

The installation of MySQL 8.0.12 took two days an...

Summary of methods to improve mysql count

I believe many programmers are familiar with MySQ...

PNG Alpha Transparency in IE6 (Complete Collection)

Many people say that IE6 does not support PNG tra...

A brief introduction to JavaScript arrays

Table of contents Introduction to Arrays Array li...

How does Zabbix monitor and obtain network device data through ssh?

Scenario simulation: The operation and maintenanc...

MySQL helps you understand index pushdown in seconds

Table of contents 1. The principle of index push-...

Linux redis-Sentinel configuration details

download Download address: https://redis.io/downl...

Solution to Ubuntu cannot connect to the network

Effective solution for Ubuntu in virtual machine ...

Detailed explanation of Django+Vue+Docker to build an interface testing platform

1. Two words at the beginning Hello everyone, my ...

JavaScript realizes the effect of mobile modal box

This article example shares the specific code of ...

Summary of the use of html meta tags (recommended)

Meta tag function The META tag is a key tag in th...

HTML table markup tutorial (4): border color attribute BORDERCOLOR

To beautify the table, you can set different bord...

Detailed explanation of MySQL string concatenation function GROUP_CONCAT

In the previous article, I wrote a cross-table up...