Validate empty object












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?










share|improve this question























  • 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 stackoverflow.com/questions/679915/…

    – Monica Acha
    Nov 22 '18 at 18:54






  • 1





    Possible duplicate of 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
















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?










share|improve this question























  • 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 stackoverflow.com/questions/679915/…

    – Monica Acha
    Nov 22 '18 at 18:54






  • 1





    Possible duplicate of 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














0












0








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?










share|improve this question














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?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 18:48









YhaLongYhaLong

32




32













  • 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 stackoverflow.com/questions/679915/…

    – Monica Acha
    Nov 22 '18 at 18:54






  • 1





    Possible duplicate of 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



















  • 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 stackoverflow.com/questions/679915/…

    – Monica Acha
    Nov 22 '18 at 18:54






  • 1





    Possible duplicate of 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

















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







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





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




1





Possible duplicate of stackoverflow.com/questions/679915/…

– Monica Acha
Nov 22 '18 at 18:54





Possible duplicate of stackoverflow.com/questions/679915/…

– Monica Acha
Nov 22 '18 at 18:54




1




1





Possible duplicate of How do I test for an empty JavaScript object?

– rlemon
Nov 22 '18 at 18:55





Possible duplicate of 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





Thanks a lot for everyone!

– YhaLong
Nov 22 '18 at 19:11












4 Answers
4






active

oldest

votes


















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;
}
}





share|improve this answer





















  • 1





    typeof's are lower case.

    – rlemon
    Nov 22 '18 at 18:54











  • Oh yeah, thanks!

    – Robson Braga
    Nov 22 '18 at 18:55











  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:00











  • I found other way to do it: if (Ext.Object.isEmpty(value1)), what do you think?

    – YhaLong
    Nov 22 '18 at 21:02











  • I don't think so those isEmpty is a native function of Object prototype, but you can make a function with that name, and assign to Object.prototype.

    – Robson Braga
    Nov 23 '18 at 12:13



















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





share|improve this answer
























  • Thanks a lot, i'm going to try this

    – YhaLong
    Nov 22 '18 at 19:05



















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;
}





share|improve this answer


























  • 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{


}





share|improve this answer


























  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436685%2fvalidate-empty-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









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;
}
}





share|improve this answer





















  • 1





    typeof's are lower case.

    – rlemon
    Nov 22 '18 at 18:54











  • Oh yeah, thanks!

    – Robson Braga
    Nov 22 '18 at 18:55











  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:00











  • I found other way to do it: if (Ext.Object.isEmpty(value1)), what do you think?

    – YhaLong
    Nov 22 '18 at 21:02











  • I don't think so those isEmpty is a native function of Object prototype, but you can make a function with that name, and assign to Object.prototype.

    – Robson Braga
    Nov 23 '18 at 12:13
















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;
}
}





share|improve this answer





















  • 1





    typeof's are lower case.

    – rlemon
    Nov 22 '18 at 18:54











  • Oh yeah, thanks!

    – Robson Braga
    Nov 22 '18 at 18:55











  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:00











  • I found other way to do it: if (Ext.Object.isEmpty(value1)), what do you think?

    – YhaLong
    Nov 22 '18 at 21:02











  • I don't think so those isEmpty is a native function of Object prototype, but you can make a function with that name, and assign to Object.prototype.

    – Robson Braga
    Nov 23 '18 at 12:13














0












0








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;
}
}





share|improve this answer















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;
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 18:55

























answered Nov 22 '18 at 18:53









Robson BragaRobson Braga

724




724








  • 1





    typeof's are lower case.

    – rlemon
    Nov 22 '18 at 18:54











  • Oh yeah, thanks!

    – Robson Braga
    Nov 22 '18 at 18:55











  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:00











  • I found other way to do it: if (Ext.Object.isEmpty(value1)), what do you think?

    – YhaLong
    Nov 22 '18 at 21:02











  • I don't think so those isEmpty is a native function of Object prototype, but you can make a function with that name, and assign to Object.prototype.

    – Robson Braga
    Nov 23 '18 at 12:13














  • 1





    typeof's are lower case.

    – rlemon
    Nov 22 '18 at 18:54











  • Oh yeah, thanks!

    – Robson Braga
    Nov 22 '18 at 18:55











  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:00











  • I found other way to do it: if (Ext.Object.isEmpty(value1)), what do you think?

    – YhaLong
    Nov 22 '18 at 21:02











  • I don't think so those isEmpty is a native function of Object prototype, but you can make a function with that name, and assign to Object.prototype.

    – Robson Braga
    Nov 23 '18 at 12:13








1




1





typeof's are lower case.

– rlemon
Nov 22 '18 at 18:54





typeof's are lower case.

– rlemon
Nov 22 '18 at 18:54













Oh yeah, thanks!

– Robson Braga
Nov 22 '18 at 18:55





Oh yeah, thanks!

– Robson Braga
Nov 22 '18 at 18:55













Thanks a lot, I'm going to try this

– YhaLong
Nov 22 '18 at 19:00





Thanks a lot, I'm going to try this

– YhaLong
Nov 22 '18 at 19:00













I found other way to do it: if (Ext.Object.isEmpty(value1)), what do you think?

– YhaLong
Nov 22 '18 at 21:02





I found other way to do it: if (Ext.Object.isEmpty(value1)), what do you think?

– YhaLong
Nov 22 '18 at 21:02













I don't think so those isEmpty is a native function of Object prototype, but you can make a function with that name, and assign to Object.prototype.

– Robson Braga
Nov 23 '18 at 12:13





I don't think so those isEmpty is a native function of Object prototype, but you can make a function with that name, and assign to Object.prototype.

– Robson Braga
Nov 23 '18 at 12:13













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





share|improve this answer
























  • Thanks a lot, i'm going to try this

    – YhaLong
    Nov 22 '18 at 19:05
















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





share|improve this answer
























  • Thanks a lot, i'm going to try this

    – YhaLong
    Nov 22 '18 at 19:05














0












0








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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 19:03









aditya agnihotriaditya agnihotri

1




1













  • Thanks a lot, i'm going to try this

    – YhaLong
    Nov 22 '18 at 19:05



















  • Thanks a lot, i'm going to try this

    – YhaLong
    Nov 22 '18 at 19:05

















Thanks a lot, i'm going to try this

– YhaLong
Nov 22 '18 at 19:05





Thanks a lot, i'm going to try this

– YhaLong
Nov 22 '18 at 19:05











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;
}





share|improve this answer


























  • 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














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;
}





share|improve this answer


























  • 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












0








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;
}





share|improve this answer















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;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 19:04

























answered Nov 22 '18 at 18:56









GhostrydrGhostrydr

568213




568213













  • 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



















  • 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

















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







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





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{


}





share|improve this answer


























  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:12
















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{


}





share|improve this answer


























  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:12














0












0








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{


}





share|improve this answer















    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{


}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 19:14

























answered Nov 22 '18 at 18:56









Banujan BalendrakumarBanujan Balendrakumar

7691212




7691212













  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:12



















  • Thanks a lot, I'm going to try this

    – YhaLong
    Nov 22 '18 at 19:12

















Thanks a lot, I'm going to try this

– YhaLong
Nov 22 '18 at 19:12





Thanks a lot, I'm going to try this

– YhaLong
Nov 22 '18 at 19:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436685%2fvalidate-empty-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'