Skip to content

Map in map JavaScript | Code

  • by

You can’t use Map in map JavaScript. Either need an array of keys for your values in the object.

Array…

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

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

Object…

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

data.key.key1;  // 'val1'

You can also simplify this using JavaScript object notation:

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

Map in map JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script >
    var adata = {};
    adata['key'] = ['val1', 'val2'];
    console.log(adata)

    var odata = {};
    odata['key'] = {key1:'val1', key2:'val2'}; 
    console.log(odata)

    var data = {};
    data.key = {val1: 'val2'};
    console.log(data)

  </script>
</body>
</html>

Output:

Map in map JavaScript | Code

Do comment if you have any doubts or suggestions on this Js map topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading