1

I need to have a equivalent of hash map in javascript. It seems Map is the right choice. However, I need to set the value of each pair in the Map as an array. For example, myMap: key1: param1 value1: [1, 2, 3]

key2: param2 value2: [3, 4, 5]

I looked at the following page, but couldn't figure out how to do that. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has

The purpose of doing this is I'm trying to extract the data in a tale on a HTML page. Basically, I want to use a hash map to organize all the data in the table. So each column name will be the key of the hash map, and the corresponding values in the column will be the value corresponding to that key.

Follows are the codes I'm trying with:

function (data) {                    
                $.each(data, function (key, value) {
                    tempData += '<tr>';
                    tempData += '<td>' + value.name + '</td>';
                    tempData += '<td>' + value.value + '</td>';

                    //check whether the key already exists in the map
                    if (measurementDataMap.get(value.name) == undefined) {
                        //if the key doesn't exist, the value points to an empty array
                        measurementDataMap[value.name] = [];
                    }
                    measurementDataMap[value.name].push(value.value);
                });

I can't figure out how to create an empty array for the value of a new pair. Thanks!

3
  • Try it! You'll find out that you can put everything into a Map including arrays. Commented Apr 1, 2019 at 23:17
  • 1
    Terminology matters: a hashmap that doesn't hash the key/value to internally store it in the appropriate bin is not a hashmap. What you're talking about needing is already covered by the basic object datatype in Javascript. let mything = { key1 : [1,2,3,4], key2: ['a', 'b', 'c', 'd']} is already perfectly fine, and perfectly working, Javascript. Commented Apr 1, 2019 at 23:17
  • I forgot to put my codes, my real question is how to create the array values on the fly. Commented Apr 1, 2019 at 23:26

2 Answers 2

6

In your code you mix up the property access syntax and the Map methods, use one of those:

const measurementDataMap = {};
//...
if(measurementDataMap[value.name] == undefined) {
    measurementDataMap[value.name] = [];
}
measurementDataMap[value.name].push(value.value);

 // OR

const measurementDataMap = new Map;
//...
if(!measurementDataMap.has(value.name))
  measurementDataMap.set(value.name, []);


measurementDataMap.get(value.name).push(value.value);
Sign up to request clarification or add additional context in comments.

Comments

3

Just a simple object:

var param1 = "myParam1", param2 = "myParam2", value1 = [1, 2, 3], value2 = [3, 4, 5];

var myObject = {
  [param1]: value1,
  [param2]: value2
};

console.log(myObject);

To fix your current code:

function(data) {
  $.each(data, function(key, value) {
    tempData += '<tr>';
    tempData += '<td>' + value.name + '</td>';
    tempData += '<td>' + value.value + '</td>';

    //check whether the key already exists in the map
    measurementDataObj[value.name] = measurementDataObj[value.name] || [];
    measurementDataObj[value.name].push(value.value);
  });
}

3 Comments

Oh yes @AmesISU - I'll add your fixed code to my answer.
that's neat! Thanks. Can you explain a little why my way wouldn't work?
You should have used map.get and map.has. You can't use [] property notation with a Map.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.