I have the following code:
var obj = {};
if (obj != null) {
//execute
}
The problem is that obj
it is not null.
How can I know if an object is empty so that it enters a condition or not?
I have the following code:
var obj = {};
if (obj != null) {
//execute
}
The problem is that obj
it is not null.
How can I know if an object is empty so that it enters a condition or not?
With
ES5
:Object.keys()
With
jQuery
:jQuery.isEmptyObject()
@aldanux's answer is correct but one option is to make it a string
JSON.stringify(obj)
we can also use
Object.entries(obj).
I suggest using Lodash. https://lodash.com/docs/4.17.15#isEmpty