Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of a JavaScript associative array

I have a JavaScript associative array (or some may prefer to call it an object) like, say

var quesArr = new Array(); quesArr["q101"] = "Your name?"; quesArr["q102"] = "Your age?"; quesArr["q103"] = "Your school?"; 

Is there a built-in function that could get the length of this array, or a solution in jQuery or another library? Currently quesArr.length would give 0, as most of you must be knowing.

Please don’t suggest iterating over the entire array/object as mentioned in this question, because the array/object which I have is very large.

Is there a way I could proceed with this?

like image 940
gopi1410 Avatar asked May 12 '12 11:05

gopi1410


People also ask

How do you find the length of an array in JavaScript?

The JavaScript array length property states the number of items in an array. To find the length of an array, reference the object array_name. length. The length property returns an integer.

What is associative array in JavaScript?

Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys. They do not have a length property like normal array and cannot be traversed using normal for loop. Following is the code for associative arrays in JavaScript −

Does JavaScript support associative array?

JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

Are JavaScript arrays fixed length?

It is not possible to create a native javascript Array with a fixed length. But, you can create an object which will behave like a fixed-length Array.


2 Answers

No, there is no built-in property that tells you how many properties the object has (which is what you're looking for).

The closest I can think of are two ES5 and higher features, Object.keys (spec | MDN) and Object.getOwnPropertyNames (spec | MDN). For instance, you could use Object.keys like this:

console.log(Object.keys(quesArr).length); // "3" 

Object.keys returns an array of the names of an object's own enumerable string-named properties. But internally (in theory) it's that loop you didn't want to use (and the polyfill for it for pre-ES5 environments uses a loop, of course). If you also want non-enumerable string-named properties, you'd use Object.getOwnPropertyNames instead.

In ES2015+, an object can have properties whose keys are Symbols rather than strings. Object.getOwnPropertySymbols (spec | MDN) lets you get them.


FWIW, unless you're going to use the Array features of the object, don't make it an array. Instead:

var quesArr = {}; quesArr["q101"] = "Your name?"; quesArr["q102"] = "Your age?"; quesArr["q103"] = "Your school?"; 

Those keys don't have to be given as string literals in square brackets, either, if you don't want them to be (whether you use an array or a plain object):

var quesArr = {}; quesArr.q101 = "Your name?"; quesArr.q102 = "Your age?"; quesArr.q103 = "Your school?"; 

But you can use the other notation if you prefer; they're exactly equivalent except that with dotted notation the keys must be valid identifier names (in bracketed notation they can be anything).

You can even do this:

var quesArr = {     q101: "Your name?",     q102: "Your age?",     q103: "Your school?" }; 

or (if the keys won't be valid identifiers):

var quesArr = {     "q101": "Your name?",     "q102": "Your age?",     "q103": "Your school?" }; 

Those can be single or double quotes.

like image 111
T.J. Crowder Avatar answered Sep 19 '22 15:09

T.J. Crowder


Suppose you have the following,

var myObject = {};  // Denotes an Object is being created myObject.city = "Chennai"; myObject.state = "Tamilnadu"; myObject.country = "Indian Peninsula"; console.log(myObject.length);  // Results in undefined 

But, there is a way to calculate the length in modern browsers (Chrome, Firefox 4+, and Internet Explorer 9):

Object.keys(myObject); // --> ["city", "state", "country"] Object.keys(myObject).length // --> 3 
like image 36
kmario23 Avatar answered Sep 18 '22 15:09

kmario23