How to create an associative array in JavaScript literal notation

JavascriptArraysAssociative Array

Javascript Problem Overview


I understand that there are no associative arrays in JavaScript, only objects.

However I can create an array with string keys using bracket notation like this:

var myArray = [];
myArray['a'] = 200;
myArray['b'] = 300;
console.log(myArray); // Prints [a: 200, b: 300]

So I want to do the exact same thing without using bracket notation:

var myNewArray = [a: 200, b: 300]; // I am getting error - Unexpected token:

This does not work either:

var myNewArray = ['a': 200, 'b': 300]; // Same error. Why can I not create?

Javascript Solutions


Solution 1 - Javascript

JavaScript has no associative arrays, just objects. Even JavaScript arrays are basically just objects, just with the special thing that the property names are numbers (0,1,...).

So look at your code first:

var myArray = []; // Creating a new array object
myArray['a'] = 200; // Setting the attribute a to 200
myArray['b'] = 300; // Setting the attribute b to 300

It's important to understand that myArray['a'] = 200; is identical to myArray.a = 200;!

So to start with what you want: You can't create a JavaScript array and pass no number attributes to it in one statement.

But this is probably not what you need! Probably you just need a JavaScript object, what is basically the same as an associative array, dictionary, or map in other languages: It maps strings to values. And that can be done easily:

var myObj = {a: 200, b: 300};

But it's important to understand that this differs slightly from what you did. myObj instanceof Array will return false, because myObj is not an ancestor from Array in the prototype chain.

Solution 2 - Javascript

You can use Map:

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

var res = arr.get('key2');
console.log(res); // The value is 'Guest'

Solution 3 - Javascript

You want to use an object in this case

var myObject = {'a' : 200, 'b' : 300 };

This answer links to a more in-depth explanation: https://stackoverflow.com/questions/1208222/how-do-i-implement-a-dictionary-or-hashtable-in-javascript

Solution 4 - Javascript

Well, you are creating an array, which is in fact an object:

var arr = [];
arr.map;
// function(..)
arr['map'];
// function(..)

arr['a'] = 5;

console.log(arr instanceof Object); // true

You can add fields and functions to arr. It does not "insert" them into the array though (like arr.push(...)).

You can refer to an object fields with the [] syntax.

Solution 5 - Javascript

Associate array is an array indexed with name similar to an object instead of numbers like in regular array. You can create an associative array in the following way:

var arr = new Array(); // OR var  arr  = [];
arr['name'] = 'david'
arr['age'] = 23;

console.log(arr['name']);

Solution 6 - Javascript

You can do what you wanted to do this way:

myNewArray = new Array ({'a' : 200, 'b' : 300})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWild WidowView Question on Stackoverflow
Solution 1 - JavascriptLuxView Answer on Stackoverflow
Solution 2 - JavascriptSanto BoldizarView Answer on Stackoverflow
Solution 3 - JavascripthttpNickView Answer on Stackoverflow
Solution 4 - JavascriptgalchenView Answer on Stackoverflow
Solution 5 - JavascriptAbhishekView Answer on Stackoverflow
Solution 6 - JavascriptmilenView Answer on Stackoverflow