xxxxxxxxxx
function isEmptyObject(obj) {
return !Object.keys(obj).length;
}
xxxxxxxxxx
const empty = {};
/* -------------------------
Plain JS for Newer Browser
----------------------------*/
Object.keys(empty).length === 0 && empty.constructor === Object
// true
/* -------------------------
Lodash for Older Browser
----------------------------*/
_.isEmpty(empty)
// true
xxxxxxxxxx
const empty = {};
/* -------------------------
Plain JS for Newer Browser
----------------------------*/
Object.keys(empty).length === 0 && empty.constructor === Object
// true
/* -------------------------
Lodash for Older Browser
----------------------------*/
_.isEmpty(empty)
// true
xxxxxxxxxx
// because Object.entries(new Date()).length === 0;
// we have to do some additional check
Object.entries(obj).length === 0 && obj.constructor === Object
xxxxxxxxxx
if(
objectName // Verify object presence
&& Object.keys(objectName).length === 0 // Verify object content
&& Object.getPrototypeOf(objectName) === Object.prototype // Verify object type
) {
// Run truthy condition.
}
xxxxxxxxxx
const obj = {};
const obj2 = { n: 1 };
function isObjectEmpty(object) {
for (const key in object) {
return !object.hasOwnProperty(key);
}
return true;
}
console.log("1", isObjectEmpty(obj));
console.log("2", isObjectEmpty(obj2));