77

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,738
  • 21
  • 105
  • 131
gopi1410
  • 6,567
  • 9
  • 41
  • 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

165

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 for plain objects (keep reading for an alternative) are things that return arrays of property keys (which you can then get length from):

  • Object.keys - Returns an array of the object's own, enumerable, string-keyed properties.
  • Object.getOwnPropertyNames - Like Object.keys, but includes non-enumerable properties as well.
  • Object.getOwnPropertySymbols - Like Object.getOwnPropertyNames, but for Symbol-keyed properties instead of string-keyed properties.
  • Reflect.ownKeys - Returns an array of an object's own properties (whether or not they're enumerable, and whether they're keyed by strings or Symbols).

For instance, you could use Reflect.ownKeys like this:

var quesArr = new Array(); // Keep reading, don't do this
quesArr["q101"] = "Your name?";
quesArr["q102"] = "Your age?";
quesArr["q103"] = "Your school?";
console.log(Reflect.ownKeys(quesArr).length); // 4 -- includes the built-in
                                              // `length` property!

Notice that showed 4, not 3, because of the built-in length property of arrays. Since you don't seem to be using the Array features of the object, don't make it an array. You could make it a plain object:

const questions = {};
questions["q101"] = "Your name?";
questions["q102"] = "Your age?";
questions["q103"] = "Your school?";
// Or just:
// const questions = {
//    q101: "Your name?",
//    q102: "Your age?",
//    q103: "Your school?",
// };
console.log(Reflect.ownKeys(questions).length); // 3

But since you want to know how many things are in it, you might consider using a Map, because unlike objects, Map instances do have a property telling you how many entries they have in them: size.

const questions = new Map();
questions.set("q101", "Your name?");
questions.set("q102", "Your age?");
questions.set("q103", "Your school?");
// Or you can pre-fill the `Map` by passing an array of `[name, value]` arrays:
// const questions = new Map([
//     ["q101", "Your name?"],
//     ["q102", "Your age?"],
//     ["q103", "Your school?"],
// ]);
console.log(questions.size); // 3
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    As you edited the answer 20 minutes ago: it's not really 2012 any longer, the **instead** part could rather culminate in [`Map.size`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size). – tevemadar Mar 06 '23 at 16:50
  • @tevemadar - LOL I literally just saw a vote on the answer, saw it didn't have `ownKeys`, and fixed just that -- I didn't even read the rest of the answer. As you say: `Map`!! I've revamped the whole thing now. As you say, things have changed in 11 years. – T.J. Crowder Mar 06 '23 at 17:14
15

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,738
  • 21
  • 105
  • 131
kmario23
  • 57,311
  • 13
  • 161
  • 150
0

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,738
  • 21
  • 105
  • 131
  • 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
-1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
viveksharma
  • 557
  • 4
  • 9
  • 1
    Please do not post your answer as an image - our firewall blocks imgur – Andrew Smith Jun 05 '19 at 14:07
  • Essentially, this boils down to the same code as TJ included in his answer half a decade before you posted this. You just expressed all the code in a barely-readable JPEG and didn't explain it at all. – Quentin Mar 06 '23 at 16:27