Home > Web Front-end > JS Tutorial > body text

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

不言
Release: 2019-04-02 09:57:54
forward
2230 people have browsed it

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
Copy after login
❗️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
}
Copy after login

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
}
Copy after login

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) === '{}'
}
Copy after login

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
}
Copy after login

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
Copy after login

[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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!