当前位置:网站首页>Several methods of checking JS to judge empty objects

Several methods of checking JS to judge empty objects

2022-07-07 12:16:00 InfoQ


Knowledge preparation

In the inventory JS Let's first understand the following three methods before judging the space .

Object.keys

Object.keys()
Method takes object as parameter , Returns an array containing the matching properties and methods in the object

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

null
You can see
objArray
For the return value , And the return value is an array with the contents of the attributes in the object

Object.getOwnPropertyNames

Object.getOwnPropertyNames()
Methods also take objects as parameters , Returns an array containing the matching properties and methods in the object

You will ask questions here , What's the difference between the two ?
Object.getOwnPropertyNames()
All properties can be returned , and
Object.keys()
Only enumerable properties can be returned , EH ? Here we are confused again
Enumerable properties
What is it again? ? Don't worry , Let me explain what an enumerable attribute is

Enumerable properties

Enumerable or non enumerable attributes are passed through enumerable flags inside objects
enumerable
To make a distinction , By default , We
obj.name = "cxy"
After adding a property to the object , Its enumerable flags
enumerable
by
ture
, And when the value is
false
It can't be enumerated , When we do
for
,
Object.keys()
,
JSON.stringify()
When enumerable attributes cannot be found , We can understand that the non enumerable attribute is
stealth
Of

Now let's take the one above
Object.getOwnPropertyNames
and
Object.keys()
Give practical examples , We go through
defineProperty
To add
age
attribute , Because this method can set the enumeration flag , I'm going to set it to
false
, You can see the following two different return results

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

null

hasOwnProperty

hasOwnProperty()
It is used to judge whether an object contains a certain attribute , Its parameter is attribute name

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

null
But one thing to pay attention to here is ,
hasOwnProperty()
Judge
Inheritance attribute
Will return
false
, Inherited properties are the properties that the object inherits from the prototype object , for instance
toString

Method of checking blank

JSON.stringify

Sentenced to empty

This method is relatively simple , Use
JSON.stringify
Convert objects to strings , Then, the Boolean value of whether the object is empty can be obtained by judging whether it is equal to

let obj = {
 name: "cxy"
}
console.log(JSON.stringify(obj) == '{}')

for in

Sentenced to empty

Use
for in
It can return when the cycle is triggered
false
When the loop is not triggered, it means that the object is null and returns
ture

let forNull = (items) => {
 for (let item in items) {
 return false
 }
 return true
}

Object.getOwnPropertyNames

Sentenced to empty

The above mentioned
Object.getOwnPropertyNames
, Of the array that will be returned
length
As a basis for judgment .

let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)

Object.keys()

Sentenced to empty

Same as the previous method , Use an array as a basis for judgment

let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)

hasOwnProperty

Sentenced to empty

Use
hasOwnProperty
It's using for The loop judges the element and returns
false
Description cannot be empty , Otherwise, it is empty

let forNull = (items) => {
 for (let item in items) {
 if(items.hasOwnProperty(item)){
 return false
 }
 }
 return true
}

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071007170214.html

随机推荐