Home  >  Article  >  Web Front-end  >  How to check if an object is empty in JavaScript (code example)

How to check if an object is empty in JavaScript (code example)

不言
不言forward
2019-04-02 09:57:542431browse

The content of this article is about how to check whether an object is empty in JavaScript (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

It is easy to check whether an array is empty. Just call the length method directly. So how to check whether an object is empty❓

The empty here refers to the object No own properties

Suppose there are two objects here, one is obj and the other is anotherObj

let obj1 = {
    name: 'oli',
    child: {
        name: 'oliver'
    }
}

let obj2 = {
    [Symbol('name')]: 'alice'
}

let obj3 = Object.defineProperty({}, 'name', {
    value: 'alice',
    enumerable: false
})

let obj4 = Object.create(null)

// 我们需要一个函数,判断是否不含自有属性

isEmpty(obj1) // false
isEmpty(obj2) // false
isEmpty(obj3) // false
isEmpty(obj4) // true
❗️I thought about it for a long time to check whether the object has Symbol attributes. You can only use the getOwnPropertySymbols method. If There is a better way, welcome to leave a message

Method 1: Traverse

for-in traversal, and confirm whether a certain key exists through the hasOwnProperty method. This method cannot Traverse to the properties whose enumerable is false

const isEmptyObj = object => {
    if (!!Object.getOwnPropertySymbols(object).length) {
        return false
    }
    for (const key in object) {
        if (object.hasOwnProperty(key)) {
            return false
        }
    }
    return true
}

Method 2: keys method

Use the Object static method keys and then determine the length. What keys returns is that it can be enumerated by itself. Attributes, so the same cannot be traversed to attributes whose enumerable is false

const isEmptyObj = object => {
    if (!!Object.getOwnPropertySymbols(object).length) {
        return false
    }
    if (Object.keys(object).length) {
        return false
    }
    return true
}

Method 3: JSON method

Use the JSON Stringify method to convert the object into a string, with characters String '{}' comparison, the same method cannot obtain the non-traversable properties

const isEmptyObj = object => {
    if (!!Object.getOwnPropertySymbols(object).length) {
        return false
    }
    return JSON.stringify(object) === '{}'
}

Method 4: getOwnPropertyNames method

Use the getOwnPropertyNames method of Object to obtain all property names , so that even non-enumerable attributes can still be obtained, which is a relatively ok method.

const isEmptyObj = object => {
    if (!!Object.getOwnPropertySymbols(object).length) {
        return false
    }
    if (!!Object.getOwnPropertyNames(object).length) {
        return false
    }
    return true
}

How to check if an object is empty in JavaScript (code example)

Simplified version:

const isEmptyObj = object => !Object.getOwnPropertySymbols(object).length && !Object.getOwnPropertyNames(object).length

[Related recommendations: JavaScript video tutorial]

The above is the detailed content of How to check if an object is empty in JavaScript (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete