DISCOVER
X

Counting Associative Array Length in JavaScript

February 26, 2013 1:47 pm Leave your thoughts
Facebooktwittergoogle_plusredditpinterestlinkedinmailFacebooktwittergoogle_plusredditpinterestlinkedinmail

Before we look at the code snippets, yes there is no such thing as associative array in JavaScript, they are all objects. But really, a lot of people search for “how do we  get JavaScript associative array length?” so I might as well publish this short post.

So say we have an object cars, containing the key  “honda”, “toyota”, “nissan” with their respective values; we can’t just get cars.length, that wouldn’t work.

We actually have to iterate over the objects and maintain the count. In the next snippet I will give both example with and without jQuery.

I hope the above helped or refreshed your memory.

Facebooktwittergoogle_plusredditpinterestlinkedinmailFacebooktwittergoogle_plusredditpinterestlinkedinmail
Tags: ,

Categorised in: ,