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

JavaScript method to determine if 'dictionary' is empty

Release: 2020-06-20 13:30:41
forward
8751 people have browsed it

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

2. Use JSON to judge.

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

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!

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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template