Map in card storage

How can I save a map on a map in javascript?

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

And I get an error about an invalid identifier.

+6
javascript map
source share
2 answers

You 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 an object ...

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

If you only need an array (list) on the data map, then what patrick dw has is fine.

If you need a map on a 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 use of JavaScript notation:

 var data = {}; data.key = {val1: 'val2'}; 
+3
source share

All Articles