59

The typical way of creating a Javascript object is the following:

var map = new Object();
map[myKey1] = myObj1;
map[myKey2] = myObj2;

I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add to the map.

Is there any way to perform something like this in Javascript:

var map =  { { "aaa", "rrr" }, { "bbb", "ppp" } ... };

or do I have to perform something like this for each entry:

map["aaa"]="rrr";
map["bbb"]="ppp";
...

Basically, remaining Javascript code will loop over this map and extract values according to criterias known 'at runtime'. If there is a better data structure for this looping job, I am interested too. My objective is to minimize code.

Ben Taliadoros
  • 7,003
  • 15
  • 60
  • 97
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • Did you actually try `var map = { { "aaa", "rrr" }, { "bbb", "ppp" } ... };` as you were asking? Because this works just fine when you put an additional key before the value... `{ key:{ "aaa", "rrr" }, key1:{ "bbb", "ppp" } }` – Christoph Feb 05 '13 at 16:18
  • possible duplicate of [How to create object property from variable value in javascript?](http://stackoverflow.com/questions/2241875/how-to-create-object-property-from-variable-value-in-javascript) – jbabey Feb 05 '13 at 16:20
  • 2
    @jbabey not really a duplicate of your proposed question but I'm pretty sure this has been asked before;) – Christoph Feb 05 '13 at 16:30

6 Answers6

166

In ES2015 a.k.a ES6 version of JavaScript, a new datatype called Map is introduced.

let map = new Map([["key1", "value1"], ["key2", "value2"]]);
map.get("key1"); // => value1

check this reference for more info.

Nitin
  • 7,187
  • 6
  • 31
  • 36
52

JavaScript's object literal syntax, which is typically used to instantiate objects (seriously, no one uses new Object or new Array), is as follows:

var obj = {
    'key': 'value',
    'another key': 'another value',
     anUnquotedKey: 'more value!'
};

For arrays it's:

var arr = [
    'value',
    'another value',
    'even more values'
];

If you need objects within objects, that's fine too:

var obj = {
    'subObject': {
        'key': 'value'
    },
    'another object': {
         'some key': 'some value',
         'another key': 'another value',
         'an array': [ 'this', 'is', 'ok', 'as', 'well' ]
    }
}

This convenient method of being able to instantiate static data is what led to the JSON data format.

JSON is a little more picky, keys must be enclosed in double-quotes, as well as string values:

{"foo":"bar", "keyWithIntegerValue":123}
Christoph
  • 50,121
  • 21
  • 99
  • 128
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
5

It works fine with the object literal notation:

var map = { key : { "aaa", "rrr" }, 
            key2: { "bbb", "ppp" } // trailing comma leads to syntax error in IE!
          }

Btw, the common way to instantiate arrays

var array = [];
// directly with values:
var array = [ "val1", "val2", 3 /*numbers may be unquoted*/, 5, "val5" ];

and objects

var object = {};

Also you can do either:

obj.property     // this is prefered but you can also do
obj["property"]  // this is the way to go when you have the keyname stored in a var

var key = "property";
obj[key] // is the same like obj.property
Christoph
  • 50,121
  • 21
  • 99
  • 128
1

Give this a try:

var map = {"aaa": "rrr", "bbb": "ppp"};
MKS
  • 352
  • 3
  • 8
1

Try this typescript

const MAP: Record<string, string> = {
  key1: 'value1'
};

MAP[this.name]
Johnny Cage
  • 5,526
  • 2
  • 11
  • 7
0

The syntax you wrote as first is not valid. You can achieve something using the follow:

var map =  {"aaa": "rrr", "bbb": "ppp" /* etc */ };
ZER0
  • 24,846
  • 5
  • 51
  • 54