Validate empty object
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
add a comment |
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
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
add a comment |
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
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
javascript
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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;
}
}
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 thoseisEmpty
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
|
show 1 more comment
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
Thanks a lot, i'm going to try this
– YhaLong
Nov 22 '18 at 19:05
add a comment |
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;
}
Depends on what you want to call an empty object. If its strictly{}
, then this doesn't suffice.Number.POSITIVE_INFINITY.isEmpty()
, for example, returnstrue
. 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
add a comment |
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{
}
Thanks a lot, I'm going to try this
– YhaLong
Nov 22 '18 at 19:12
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
}
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 thoseisEmpty
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
|
show 1 more comment
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;
}
}
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 thoseisEmpty
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
|
show 1 more comment
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;
}
}
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;
}
}
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 thoseisEmpty
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
|
show 1 more comment
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 thoseisEmpty
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
|
show 1 more comment
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
Thanks a lot, i'm going to try this
– YhaLong
Nov 22 '18 at 19:05
add a comment |
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
Thanks a lot, i'm going to try this
– YhaLong
Nov 22 '18 at 19:05
add a comment |
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
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
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
add a comment |
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
add a comment |
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;
}
Depends on what you want to call an empty object. If its strictly{}
, then this doesn't suffice.Number.POSITIVE_INFINITY.isEmpty()
, for example, returnstrue
. 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
add a comment |
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;
}
Depends on what you want to call an empty object. If its strictly{}
, then this doesn't suffice.Number.POSITIVE_INFINITY.isEmpty()
, for example, returnstrue
. 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
add a comment |
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;
}
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;
}
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, returnstrue
. 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
add a comment |
Depends on what you want to call an empty object. If its strictly{}
, then this doesn't suffice.Number.POSITIVE_INFINITY.isEmpty()
, for example, returnstrue
. 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
add a comment |
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{
}
Thanks a lot, I'm going to try this
– YhaLong
Nov 22 '18 at 19:12
add a comment |
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{
}
Thanks a lot, I'm going to try this
– YhaLong
Nov 22 '18 at 19:12
add a comment |
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{
}
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{
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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