Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map in map storing

Tags:

javascript

map

how can I store map in map in javascript?

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

And I get an error about wrong id.

like image 211
Max Frai Avatar asked Feb 16 '11 18:02

Max Frai


People also ask

How do I store a Map on another Map?

Method 1: By normally iterating and putting it to another HashMap using put(k, v) Method. A simple solution is to iterate through the map and use put(Key,Value) once for each mapping key and value in the another Map.

Can you put a Map in a Map Java?

HashMap put() Method in Java put() method of HashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value.

Can you have a Map in a Map C++?

Implementing Multidimensional Map in C++ The key can be of any data type, including those that are user-defined. Multidimensional maps are nested maps; that is, they map a key to another map, which itself stores combinations of key values with corresponding mapped values.

Can I have a HashMap in a HashMap?

Flatten a Nested HashMap One alternative to a nested HashMap is to use combined keys. A combined key usually concatenates the two keys from the nested structure with a dot in between. For example, the combined key would be Donut.


2 Answers

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'
like image 101
user113716 Avatar answered Oct 06 '22 00:10

user113716


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'};
like image 32
Platinum Azure Avatar answered Oct 06 '22 00:10

Platinum Azure