gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
check object empty
- User
check object empty
- Assistant
In JavaScript, you can check if an object is empty by verifying that it has no properties. One common way to do this is by using the
Object.keys()method, which returns an array of an object's own enumerable property names. If this array's length is 0, the object is considered empty.Here's a simple function to check if an object is empty:
javascriptfunction isObjectEmpty(obj) { return Object.keys(obj).length === 0 && obj.constructor === Object; } // Example usage: const obj1 = {}; const obj2 = { key: 'value' }; console.log(isObjectEmpty(obj1)); // true console.log(isObjectEmpty(obj2)); // falseExplanation:
- gets all the keys of the object.