Get the App
SLTechnology News&Howtos  ›  Development  › 

How to determine whether an object is empty in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 17:45:30 09月11日 Update

This article will explain in detail how to judge whether the object is empty in JavaScript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

In your daily development, you usually use Object.keys () to determine whether the object is empty:

Const obj = {}; Object.keys (obj). Length = = 0 / / true is an empty object

But in one article, it was judged like this:

Const obj = {}; Object.keys (obj). Length = = 0 & & obj.constructor = Object

Why judge the constructor of an object in addition? What about the constructor?

Object.keys ()

Introduction to Object.keys () in MDN:

The Object.keys () method returns an array of enumerable properties of a given object.

Example var arr = ['aqu,' baked,'c']; console.log (Object.keys (arr)); / / ['0,'1,'2'] var obj = {0: 'axiom, 1:' baked, 2:'c'}; console.log (Object.keys (obj); / / ['0,'1,'1,'2'] Why do you need an additional constructor to determine the object?

The author of the article wrote that it was to cover the wrapper instance (I was a little confused at the time), which was explained later. In JavaScript, there are nine built-in constructors.

New Object (); new String (); new Number (); new Boolean (); new Array (); new RegExp (); new Function (); new Date (); new Error ()

We can use these constructors to create objects, such as new Object () (not recommended on a daily basis).

Const obj = new Object (); Object.keys (obj). Length = 0; / / true

Is there nothing wrong with the usual method of judgment?

But there is a problem when you create it using the remaining built-in constructors.

Function badEmptyCheck (value) {return Object.keys (value). Length = 0;} badEmptyCheck (new String ()); / / truebadEmptyCheck (new Number ()); / / truebadEmptyCheck (new Boolean ()); / / truebadEmptyCheck (new Array ()); / / truebadEmptyCheck (new RegExp ()); / / truebadEmptyCheck (new Function ()); / / truebadEmptyCheck (new Date ()); / / truebadEmptyCheck (new Error ();); / / true

Don't you get it?

After adding constructor judgment:

Function goodEmptyCheck (value) {return Object.keys (value). Length = = 0 & & value.constructor = Object;} goodEmptyCheck (new String ()); / / falsegoodEmptyCheck (new Number ()); / / falsegoodEmptyCheck (new Boolean ()); / / falsegoodEmptyCheck (new Array ()); / / falsegoodEmptyCheck (new RegExp ()); / / falsegoodEmptyCheck (new Function ()); / / falsegoodEmptyCheck (new Date ()); / / falsegoodEmptyCheck (new Error ();); / / false

The result is correct, in fact, we have dealt with the boundary situation.

Another boundary case, Object.keys (null). Length = 0; / / TypeErrorObject.keys (undefined). Length = 0; / / TypeError

The solution is also simple:

Function goodEmptyCheck (value) {return value & & Object.keys (value). Length = = 0 & & value.constructor = Object;} this is the end of the article on "how to determine whether an object is empty in JavaScript". I hope the above content can be of some help to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Objects functions articles methods situations articles more boundaries problems processing good practical ordinary clear author content instance actual actually properties Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Apple Xiaomi macOS