Sometimes, we want to get the number of elements in a JavaScript object.
In this article, we’ll look at how to get the number of elements in a JavaScript object.
How to get the number of elements in a JavaScript object?
To get the number of elements in a JavaScript object, we can use the Object.keys method.
For instance, we write
const count = Object.keys(obj).length;
to call Object.keys with obj to get an array with the key strings in obj.
Then we use the length property to get the number of properties in obj.
Conclusion
To get the number of elements in a JavaScript object, we can use the Object.keys method.