javascript check if the attribute of an object exists

Asked

Viewed 20,769 times

4

I have an object that I receive, and sometimes it comes with an attribute other times no, my problem is in sequence when I go to work with it see the example below:

if( json.aviso != ""  ){ 
                    //...fazer alguma coisa 
                }

Only when the object does not come with the attribute json.warning it performs what is inside the if, I believe because null is different from "" would have a more elegant way of treating that of doing as:

if( json.aviso != "" && json.aviso !=null ){  //...me parece gambiarra }
  • 2

    You can do it this way: if ((json.hasOwnProperty("aviso")) && (json.aviso !== ""))

3 answers

6

There are 3 situations you need to pay attention to:

  1. The attribute does not exist or its value is undefined;
  2. The attribute exists and its value is null;
  3. The attribute exists and its value is "falsy" (false, 0, "", etc.).

The most guaranteed way to differentiate between cases is by using the operators === and !==, because they take the type of operands into consideration beyond their values, while == and != have comparison rules that are not always intuitive.

Some examples:

var obj = { a:"foo", b:"", c:42, d:0, e:true, f:false, g:null, h:undefined };

[].forEach.call("abcdefghi", function(prop) {
    document.body.innerHTML += "<p>Testando: " + prop + " (" + obj[prop] + ")</p>";

    testa(prop, " === undefined");
    testa(prop, " === null");
    testa(prop, " === false");
    testa(prop, ' === ""');
    testa(prop, ' === 0');

    testa(prop, " == undefined");
    testa(prop, " == null");
    testa(prop, " == false");
    testa(prop, ' == ""');
    testa(prop, ' == 0');
});

function testa(prop, expr) {
    if ( eval("obj['" + prop + "'] " + expr) )
        document.body.innerHTML += "<p>obj." + prop + " " + expr + "</p>";
}

My suggestion is to list the cases where your condition applies and combine them with &&, just like you’re doing. Whether it’s gambiarra or not, I can’t say, but if it is, it’s a matter of necessity imposed by language...


P.S. You may be wondering: "but how to differentiate between the case where an attribute does not actually exist and the case where it exists but its value is undefined?". Well, I’m wondering the same thing, but I don’t recall there being a means to it.

In the case of variables it is possible (see question "How to check Undefined correctly in Javascript" for more details), but in the case of object properties, I fear not (even converting the object to JSON will make the existing properties but with value undefined disappear from the result - because JSON does not support undefined).

3

You can do it this way:

if ((json.hasOwnProperty("aviso")) && (!!json.aviso))

This way it will first check whether the object whose name is json exists the property aviso and finally will verify the value of it, so you guarantee that is being verified the existence of the property and the value of it.

Edit:

There is an even more effective way that is just to leave the !!json.aviso

if (!!json.aviso)

0

if(json.aviso) {
   //code
}

however, if the value of your notice is equal to "", will not fall into your if

It is also worth noting that: This solution is only 100% valid in cases where the value contained within warning is not numerical, because when the value is numerical and it has the value 0 the code within the if clause is not executed

  • This solution is only 100% valid in cases that the value contained within aviso is not numeric, because when the value is numeric and it has the value 0 the code within the if clause is not executed

  • humm, really.. .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.