Categories
JavaScript Answers

How to get the length of a JavaScript associative array?

Spread the love

To get the length of a JavaScript associative array, we use the Object.keys method.

For instance, we write

const quesArr = {};
quesArr["q101"] = "Your name?";
quesArr["q102"] = "Your age?";
quesArr["q103"] = "Your school?";
console.log(Object.keys(quesArr).length);

to call Object.keys to get the property keys of quesArr in an array.

Then we use the array’s length property to get the length of the associative array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *