I have this object:
{"": undefined}
and when I check this object for empty in this way:
_.isEmpty({"": undefined})
I get false result, maybe in lodash we have another method?
_.isEmpty(obj, true)
var obj = {
'firstName': undefined
, 'lastName' : undefined
};
console.log(_.isEmpty(obj)); // false
console.log(_.isEmpty({})); // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
Please, see http://www.ericfeminella.com/blog/2012/08/18/determining-if-an-object-is-empty-with-underscore-lo-dash/
I guess this is a bit overkill, but this is what I use which recursively checks for nested objects too and uses lodash.
function checkEmptyObject(obj) {
if (_.isEmpty(obj)) return true;
return _.isEmpty(
Object.entries(obj)
.map(([key, value]) => {
if (_.isEmpty(value)) return true;
if (value instanceof Object) return checkEmptyObject(value);
return false;
})
.filter((b) => b === false)
);
}
{}is an empty object. - MForMarlon