Empty Object
Today I learned this code doesn't work as I think it would.
objectName !== {} ? displayObject : null
The correct ways to check for empty objects are:
- use
Object.keys
Note: We can also check this usingObject.keys(obj).length === 0Object.values
andObject.entries
. - use JSON.stringifyJSON.stringify(obj) === '{}'
- Looping over object properties with for…in
function isEmpty(obj) { for(let prop in obj) { if(obj.hasOwnProperty(prop)) return false; } return true;}
Source -> More ways to check