xxxxxxxxxx
// because Object.keys(new Date()).length === 0;
// we have to do some additional check
Object.keys(obj).length === 0 && obj.constructor === Object
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
function isEmptyObject(obj) {
// Check if object has any properties
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
return false; // Object has at least one property
}
}
return true; // Object is empty
}
// Example usage
var myObject = {};
console.log(isEmptyObject(myObject)); // Output: true
var anotherObject = { name: "John", age: 25 };
console.log(isEmptyObject(anotherObject)); // Output: false
xxxxxxxxxx
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;
xxxxxxxxxx
def is_object_empty(obj):
if not obj: # Checks if the object is empty
return True
return False
# Example usage
empty_object = {}
non_empty_object = {'key': 'value'}
print(is_object_empty(empty_object)) # Output: True
print(is_object_empty(non_empty_object)) # Output: False
xxxxxxxxxx
def is_object_empty(obj):
if not obj:
return True
if isinstance(obj, dict):
return len(obj) == 0
return len(obj.__dict__) == 0
# Example Usage
empty_obj = {}
non_empty_obj = {"key": "value"}
print(is_object_empty(empty_obj)) # True
print(is_object_empty(non_empty_obj)) # False
xxxxxxxxxx
// Today, end of 2021 near, I'd go for
const objIsEmpty = o => !Reflect.ownKeys( o ).length;
// Will catch also non-empties with Symbol keys, e.g.
const FOO = Symbol('foo');
const obj = { [FOO]: 'foo', };
objIsEmpty(obj); // false
objIsEmpty({}); // true