Friday, February 23, 2024
rated 0 times [  119] [ 1]  / answers: 1 / hits: 148262  / 11 Years ago, tue, february 5, 2013, 12:00:00

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:


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.

More From » object-literal


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 = [
'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}

[#80405] Monday, February 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 117
Total Questions: 116
Total Answers: 116

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
maxinec questions