Any data structure with key, value but key not unique?

I am looking for a data structure with key and value (Map or Dictionary) but key is not unique in Node-Red.
I have tried with this

var map1 = new Map();
map1.set("test", "value");

but it doesn't work at all.

Hi @torrent

can you explain a bit more what you mean?

Do you mean you want to have multiple values for a single key?

The code you've shared is perfectly valid JavaScript. What do you mean it doesn't work at all? What do you want it to do?

You are right. I want multiple values for a single key.
The code I've shared doesn't work when I run with debug (return msg already). It doesn't print anything out! msg is empty!

There is no native JavaScript data structure that does that, but it is easy enough to do with a plain object where you ensure its values are arrays that contain 1 or more elements:

let map = {};
let key = "test";
let value = "value";

// Ensure that map[key] is an array if not already defined
map[key] = map[key] || [];
// Append the value to the array held in map[key]
1 Like