how can I store map in map in javascript?
var data = {};
data['key'] = {'val1', 'val2'};
And I get an error about wrong id.
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.
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.
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.
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.
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'
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'};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With