Length of a JavaScript Associative Array

Length of a JavaScript associative array

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.

Javascript associative array does not support Array.prototype.map function

JavaScript doesn't have a feature called "associative arrays".

It has objects, and arrays are a type of object designed to hold numerically indexed values. (The length property on an array is calculated based on the highest numbered numerical property).

(Since arrays are a type of object, you can store arbitrary properties on them, but this is not a good practice).

If you want to store named properties, then do not use an array. Use a plain object or a Map. These serve the same purposes as associative arrays in other languages.

You can count the enumerated properties of an object by extracting them into an array and then checking its length.

var myObject = {};myObject.one = 1;console.log(Object.keys(myObject).length);

Max char length of a js associative array on IE8

It was an console limitation in IE8, not an array limitation.

Why is the length of an array 0 after a assign a key/value pair? (JS)

You need to push value to array.

array.push({name:'peter'});

json.length() associative array format

The reason json_encode would return the latter format is if not all keys are continuously ordered numerical. The reason being that PHP only has one array type, but this maps to either a JSON [] array or {} object, depending on what keys it contains. That means you're somehow futzing up the keys in the array before you encode it. Your fix should be there, not on the Javascript side.

Array as map returns wrong length

JavaScript is not supported associative array so use object and get size by property name array size with the help of Object.keys() method.

// initialize as objectvar f = {};
// define propertiesf["1_f_1"] = "1";f["2_f_2"] = "2";f["3_f_3"] = "3";
// get object property name array and get lengthalert(Object.keys(f).length);


Related Topics



Leave a reply



Submit