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

How to check whether an object is empty with Javascript

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Javascript to check whether the object is empty". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Javascript to check whether the object is empty".

Methods: 1, convert the object into a json string to determine whether the string is "{}"; 2, use the "$.isEmptyObject" statement; 3, use the "Object.getOwnPropertyNames" statement; 4, use "Object.keys".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript checks whether the object is empty

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. IsEmptyObject method of jquery

This method is encapsulated by jquery method 2 (for in), and it depends on jquery when using it.

Var data = {}; var b = $.isEmptyObject (data); alert (b); / / true

3. Object.getOwnPropertyNames () method

This method uses the getOwnPropertyNames method of the Object object to get the property name in the object, save it in an array, and return the array object. We can judge whether the object is empty by judging the length of the array.

Note: this method is not compatible with ie8. Other browsers have not tested it.

Var data = {}; var arr = Object.getOwnPropertyNames (data); alert (arr.length = = 0); / / true

4. Use the Object.keys () method of ES6

Similar to method 3, it is a new method of ES6, and the return value is also an array of property names in the object.

Var data = {}; var arr = Object.keys (data); alert (arr.length = = 0); / / true thank you for reading, the above is "how to use Javascript to check whether the object is empty", after the study of this article, I believe you have a deeper understanding of how to use Javascript to check whether the object is empty, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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