71

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?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
gopi1410
  • 6,358
  • 8
  • 40
  • 75
  • I would have suggested you to iterate over the entire object as this is the only way to achieve this. What you have is an Object (which was an Array before the second line). An object doesn't have a length, an Array has. – Fabien Ménager May 12 '12 at 11:24
  • 2
    Possible duplicate of [Length of a JavaScript object (that is, associative array)](http://stackoverflow.com/questions/5223/length-of-a-javascript-object-that-is-associative-array) – Malvineous Sep 10 '16 at 11:34

4 Answers4

157

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.

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
  • 1
    ok, this is some new information for me. & also I see there is no way to know the length of an object without iterating over it. Anyways thanks. :) – gopi1410 May 12 '12 at 11:30
  • 4
    @gopi1410: Oh, btw, any time you find yourself writing `new Array()`, you can use `[]` instead. `[]` creates a new, blank array, just like `{}` creates a new, blank object. – T.J. Crowder May 12 '12 at 11:32
13

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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
kmario23
  • 50,454
  • 13
  • 141
  • 141
1

One can also deal with the issue of associative arrays length matters by keeping track of the length of the array with a associative element called length.

As known as:

myArray["length"] = 0;

As you add or remove elements simply add or subtract from the myArray["length"] element.

Then you can do things such as:

theLength = myArray.length;
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
  • In the OP's case their array is an actual array, not a simple object, so it already has a `.length` property. So if they happened to do any "proper" array operations (e.g., `.push()`) that would change the length... – nnnnnn Jul 25 '16 at 06:03
0

An associative array does not have the length property, but you can get length.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
viveksharma
  • 359
  • 2
  • 7