admin管理员组

文章数量:1136200

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?

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?

Share Improve this question edited Jul 10, 2020 at 18:30 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked May 12, 2012 at 11:20 gopi1410gopi1410 6,62510 gold badges43 silver badges76 bronze badges 2
  • 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 Commented May 12, 2012 at 11:24
  • 2 Possible duplicate of Length of a JavaScript object (that is, associative array) – Malvineous Commented Sep 10, 2016 at 11:34
Add a comment  | 

4 Answers 4

Reset to default 166

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

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

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;

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

本文标签: Length of a JavaScript associative arrayStack Overflow