Languages

JavaScript - how to get length of associative array?

0 points
Asked by:
Violetd
925

How can I get the length of an associative array in JavaScript?

I have an associative array (or as some say - an object) like this:

Is there any built-in function that could get the length of this array without iterating it?

I tried items.length but returns 0.

1 answer
0 points
Answered by:
Admin Dirask Community
4380

There is no built-in method that returns the number of properties the object has.

However, you can use Object.keys() method to get an array of property names and then get the length of it like this:

Note:

Object.keys() returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.

 

References

  1. Object.keys() - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join