Max Frai
Max Frai

Reputation: 64266

Map in map storing

how can I store map in map in javascript?

var data = {};
data['key'] = {'val1', 'val2'};

And I get an error about wrong id.

Upvotes: 5

Views: 12744

Answers (2)

Platinum Azure
Platinum Azure

Reputation: 46183

If you want just an array (a list) in the data map, what patrick dw has is fine.

If you want a map in your data map, you need the following:

var data = {};
data['key'] = {'val1': 'val2'}; // using a colon instead of a comma to create key-value pairing

You can also simplify this using JavaScript object notation:

var data = {};
data.key = {val1: 'val2'};

Upvotes: 3

user113716
user113716

Reputation: 322492

You either need an array...

var data = {};
data['key'] = ['val1', 'val2'];  // store an Array at data.key

data.key[0]; // 'val1'

...or keys for your values in the object...

var data = {};
data['key'] = {key1:'val1', key2:'val2'};  // store an Object at data.key 

data.key.key1;  // 'val1'

Upvotes: 14

Related Questions