Sometimes, we want to match an empty dictionary in JavaScript.
In this article, we’ll look at how to match an empty dictionary in JavaScript.
How to match an empty dictionary in JavaScript?
To match an empty dictionary in JavaScript, we can use the Object.keys method.
For instance, we write
const isEmpty = (obj) => {
return Object.keys(obj).length === 0;
};
to define the isEmpty function.
In it, we call Object.keys on object obj to return an array of non-inherited string property keys.
Then we get the length of the array and check if it’s 0 to check if we have an empty object.
Conclusion
To match an empty dictionary in JavaScript, we can use the Object.keys method.