0

I have an variable that devolves an empty object, and i need validate thas this variable have a value.

guardar: function() {
    var value1 = Ext.getCmp('radio1').getValue();

    if (value1 === {}) {
        alert('It is necessary to select an option.');
        return;
    } 
}

When it arrives in the debug line in the If statement, although the value of the variable is {}, when evaluating the condition the result is false.

¿Someone could help me about how can i do that validation?

YhaLong
  • 3
  • 2
  • you can get the length of a list of keys on the object. `Object.keys(value1).length`, === failed because it's checking identity, and the two objects are not the same object. – rlemon Nov 22 '18 at 18:52
  • Check the extjs documentation on how `.getCmp()` works, what it returns and how to determine if the returned component has a "value" – Andreas Nov 22 '18 at 18:53
  • 1
    Possible duplicate of https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object – Monica Acha Nov 22 '18 at 18:54
  • 1
    Possible duplicate of [How do I test for an empty JavaScript object?](https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object) – rlemon Nov 22 '18 at 18:55
  • Thanks a lot for everyone! – YhaLong Nov 22 '18 at 19:11

4 Answers4

0

If you get a object, then, do you need to proccess that object to verify if exist a property Try this:

guardar: function() {
    var value1 = Ext.getCmp('radio1').getValue();

    if (typeof value1 === 'object' && Object.keys(value1).length === 0) {
        alert('It is necessary to select an option.');
        return;
    } 
}
Robson Braga
  • 392
  • 1
  • 7
0

You can't do value1 === {} for the same reason that {} === {} is false. The reason for this is javascript compares objects by reference, not by value. Which means it checks if the objects occupy the same memory location.

You can try something like

Object.prototype.isEmpty = function() {
    for(var key in this) {
        if(this.hasOwnProperty(key))
            return false;
    }
    return true;
}

Then you can test if it's empty

if (value1.isEmpty()) {
    alert('It is necessary to select an option.');
    return;
} 
Ghostrydr
  • 594
  • 2
  • 13
  • Depends on what you want to call an empty object. If its strictly `{}`, then this doesn't suffice. `Number.POSITIVE_INFINITY.isEmpty()`, for example, returns `true`. But then again, this may be good enough. I would avoid extending built-in objects though. – Caramiriel Nov 22 '18 at 19:11
  • Thanks a lot, I'm going to try this – YhaLong Nov 22 '18 at 19:11
0
    function isEmpty(obj) {
        for(var key in obj) {
            if(obj.hasOwnProperty(key)){
                return false;
            }
        }
        return true;
    }

    var x = {};

   if(isEmpty(x)){

     alert('It is necessary to select an option.');
     return;

   }else{


   }
BadPiggie
  • 3,374
  • 1
  • 7
  • 23
0
guardar: function() {

var value1 = Ext.getCmp('radio1').getValue();

if (Object.keys(value1).length === 0) {
    alert('It is necessary to select an option.');
    return;
 } 
} // This Will Work as your requirement