Home >Web Front-end >JS Tutorial >JavaScript method to determine if 'dictionary' is empty

JavaScript method to determine if 'dictionary' is empty

尚
forward
2020-06-20 13:30:418824browse

JavaScript method to determine if 'dictionary' is empty

Many people will misunderstand

1. JavaScript should not have a dictionary.

2. If JavaScript says If an object is empty, then the object should be null.

For example: var obj = {}; This cannot be called the object is empty, you can only say that the object has no attributes.

Memory It's still occupied. If you don't believe it, you can try typeof(obj).

3. Generally, we don't judge directly like Python. We usually look to see if there is a certain attribute in it.

For example, obj.name can be judged directly. Furthermore, generally we use this thing, directly using $.echo(), which is simple and crude.

Method of judgment

After talking about the above things, if you insist on making a judgment, I will not stop you. I will give you two methods.

1. Attributes in the loop.

function isEmptyObject(obj){
    for (var n in obj) {
        return false
    }
    return true; 
}

2. Use JSON to judge.

function isEmptyObject(obj){
    if (JSON.stringify(obj) == '{}') {
        return true;
    } else {
        return false;
    }
}

For more JavaScript knowledge, please pay attention to the PHP Chinese websiteJavaScript video tutorialcolumn

The above is the detailed content of JavaScript method to determine if 'dictionary' is empty. 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