Get the App
SLTechnology News&Howtos  ›  Development  › 

How to judge whether an object is null in es6

Shulou Source: shulou.com Published: 2022-05-31 18:39:51 10月23日 Update

This article mainly introduces the relevant knowledge of "how to judge whether the object is null in es6". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope that this article "how to judge whether the object is null in es6" can help you solve the problem.

In es6, you can use the "Object.keys ()" method to determine whether an object is null, which returns an array of enumerable attributes of the object itself, with the syntax of "alert (Object.keys). Length = = 0", and if the result is true, the object is null.

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

How to judge whether the object is null in es6

Use the Object.keys () method of ES6

The Object.keys () method returns an array of enumerable properties of a given object, and the return value is also an array of property names in the object.

Examples are as follows:

Var data = {}; var arr = Object.keys (data); alert (arr.length = = 0); / / true is the empty object var datas= {aVO1LI BLAR 2}; var aRR = Object.keys (datas); console.log (aRR)-- > ['aplomb']

Among them, it should be noted that:

If data = = null will report an error, you need to make sure the value is not null or undefined

A brief introduction to the Object.keys () method

Pass in an object and return the property name

Var data= {avell 1, Object.keys brew 2); console.log (Object.keys (data)); / / ["a", "b", "c", "d", "e"] Object.keys (data). Map ((key,item) = > {console.log (key,data [key])); / / key= > attribute name data [key] = > attribute value})

Pass in a string and return the index

Var str = 'ab1234';console.log (Object.keys (obj)); / / [0Jing 1jue 2m 3jue 4je 5]

Incoming array returns index

Var arr = ["a", "b", "c"]; console.log (Object.keys (arr)); / / console: ["0", "1", "2"] this is the end of the introduction of "how to determine whether an object is null in es6". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Objects properties methods arrays knowledge indexes industries different practical content characters strings practicality practice tutorials articles more cases environments computers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Shulou Tech Info MySQL Microsoft