Categories
JavaScript Answers

How to create an associative array in JavaScript literal notation?

Spread the love

To create an associative array in JavaScript literal notation, we can create a map.

For instance, we write

const arr = new Map([
  ["key1", "User"],
  ["key2", "Guest"],
  ["key3", "Admin"],
]);

const res = arr.get("key2");
console.log(res);

to create an associative array with the Map constructor.

Then we call get with the key to get the value associated with the key.

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 *