Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of judging empty objects by JS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what JS methods to judge empty objects, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Object.keys

The Object.keys () method takes an object as a parameter and returns an array of properties and methods that match within that object.

Var obj = {name: "cxy", age: "19"}; var objArray = Object.getOwnPropertyNames (obj); console.log (objArray)

You can see that objArray is the return value, and the return value is an array of properties within the object

Object.getOwnPropertyNames

The Object.getOwnPropertyNames () method also takes an object as a parameter and returns an array of properties and methods that match within that object.

You will ask here, what's the difference between the two? Object.getOwnPropertyNames () can return all properties, while Object.keys () can only return enumerable properties, eh? At this point, people are wondering, but what is the enumeration attribute? Don't worry, let me explain what an enumerable attribute is.

Enumerable attribute

Enumerable or non-enumerable attributes are distinguished by the enumerable flag enumerable inside the object. By default, after we obj.name = "cxy" add a property to the object, the enumerable flag enumerable is ture, but when its value is false, it cannot be enumerated. When we perform for,Object.keys () and JSON.stringify () on the object, the non-enumerable attribute cannot be found. We can understand that non-enumerable attributes are invisible.

Now let's take the above Object.getOwnPropertyNames and Object.keys () for practical examples. We use defineProperty to add the age property to the object, because this method can set the enumeration flag, which is set to false, and you can see the following two different results

Var stuObj = {name: "cxy"} Object.defineProperty (stuObj, 'age', {value: "18", enumerable: false}); console.log (Object.keys (stuObj)) console.log (Object.getOwnPropertyNames (stuObj))

HasOwnProperty

HasOwnProperty () is used to determine whether an object contains an attribute, and its parameter is the attribute name.

Var stuObj = {name: "cxy"} console.log (stuObj.hasOwnProperty ('name'))

But one problem to note here is that hasOwnProperty () returns false when judging inheritance properties, which are properties that objects inherit from prototype objects, such as toString

Inventory check null method JSON.stringify null method

This method is relatively simple. Use JSON.stringify to convert the object into a string, and then determine whether the object is empty by determining whether the Boolean value is empty.

Let obj = {name: "cxy"} console.log (JSON.stringify (obj) = ='{}') for in is null

If you use for in, you can return false when the loop is triggered. If the loop is not triggered, it means the object is empty and returns ture.

Let forNull = (items) = > {for (let item in items) {return false} return true} Object.getOwnPropertyNames is null

The Object.getOwnPropertyNames mentioned above is used here, and the length of the returned array is used as the basis for judgment.

Let stuArray = Object.getOwnPropertyNames (obj) console.log (stuArray.length = 0) Object.keys () null

As in the previous method, use the array as the basis for judgment

Let stuArray = Object.getOwnPropertyNames (obj) console.log (stuArray.length = 0) hasOwnProperty is null

Using hasOwnProperty is to use the for loop to judge the element. If it contains it, the return false statement is not empty, otherwise it is empty.

Let forNull = (items) = > {for (let item in items) {if (items.hasOwnProperty (item)) {return false}} return true} is attached to convert an object to a string for comparison

This method is not recommended, but it is really the easiest to think of. It mainly uses the method of JSON.stringify () to forcibly turn the object and post it for a look:

Var a = {}; var b=new Object (); console.log ("comparison of literals of objects:" + (JSON.stringify (a) = = "{}") console.log ("comparison of constructors:" + (JSON.stringify (b) = = "{}")) these are all the contents of the article "what are JS's methods for judging empty objects?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report