Categories
JavaScript Answers

How to get a list of associative array keys with JavaScript?

Spread the love

To get a list of associative array keys with JavaScript, we call the Object.keys method.

For instance, we write

const dictionary = {
  cats: [1, 2, 37, 38, 40, 32, 33, 35, 39, 36],
  dogs: [4, 5, 6, 3, 2],
};

const keys = Object.keys(dictionary);
console.log(keys);

to call Object.keys with dictionary to return a list of key strings from the dictionary object.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.