This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

In the process of front-end development, data processing is a very common operation, and with data processing as the entry point, there are a lot of relevant knowledge points for front-end developers to understand and learn. In JS to determine whether the object (obj) is empty is also a common operation, so this article will share about the way to determine the format of the object.

extended

First of all, let’s take a look at a more classic example of js judging whether the object is empty. The specific content is as follows:

Js determines whether an object is empty:

If (obj) = false when obj=””,obj=undefined,obj=null,obj=NaN;

If (obj) is true if obj is not null.

Does it seem easy after reading the online example above, but if you follow the steps above, will you be successful? Please see the officer first test whether it can, here will not demonstrate the final result.

The core content

Let’s talk about a more professional method, as shown below.

There are five ways to determine if an object is empty. Three of the most popular methods are json.stringify () and for… In circulation, the Object. The keys (), the remaining two for: jquery isEmptyObject method, Object. GetOwnPropertyNames () method. This article will only explain the three commonly used methods to determine whether an object object is empty. The remaining two methods will not be introduced here.

Method 1: json.stringify () to determine whether the object (obj) is empty.

Use json.stringify () to convert a JSON object to a JSON string and determine if the string is “{}”, which tells you if the object is empty.

var data = {}; var a = (JSON.stringify(this.projectData) == "{}"); If (a === true)){// If data is empty, return true; }Copy the code

For… In loop judgment

Through the for… In loop to determine if object (obj) is empty, directly through for… In loop to process the object.

var data = {}; var a = function() { for(var key in data) { return false; } return true; } alert(a()); // True, data is an empty objectCopy the code

Keys () uses object.keys () to determine whether an Object (obj) is empty. This method is also new in ES6. The return value is an array of property names in the Object.

var data = {}; var array = Object.keys(data); If (array.length === 0){// If the array length is 0, true is returned, indicating that the data object is empty. }Copy the code

The sample

Finally, give a simple example of actual combat, at a glance to use JS to determine whether the object (OBj) is empty, eg:

nextClick() { console.log("this.projectData",this.projectData); ProjectData var tmpStr = (json.stringify (this.projectData) == "{}"); // Convert this.projectData to json format based on the printout of this.projectData, and then use the converted format directly. If (tmpStr == true) {//true = null this.$toast(" Please select! ); return; }},Copy the code

The last

Through the above introduction, in the front-end development of the object (OBJ) is empty is very simple, although the knowledge point is not complex, but very practical, especially for just contact front-end developers very practical.

The above is all the content of this chapter. Welcome to pay attention to the wechat public account of Sanzhan “Program Ape by Sanzhan”, and the Sina Weibo account of Sanzhan “Sanzhan 666”, welcome to pay attention!