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

**URL:** <https://discourse.nodered.org/t/any-data-structure-with-key-value-but-key-not-unique/35766>\
**Category:** General\
**Created:** [11 November 2020 21:05 UTC](https://discourse.nodered.org/t/any-data-structure-with-key-value-but-key-not-unique/35766 "2020-11-11T21:05:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [11 November 2020 21:05 UTC](https://discourse.nodered.org/t/any-data-structure-with-key-value-but-key-not-unique/35766/1 "2020-11-11T21:05:23Z")

</div>

Hi,  
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

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

```

but it doesn't work at all.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 November 2020 21:18 UTC](https://discourse.nodered.org/t/any-data-structure-with-key-value-but-key-not-unique/35766/2 "2020-11-11T21:18:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [11 November 2020 21:23 UTC](https://discourse.nodered.org/t/any-data-structure-with-key-value-but-key-not-unique/35766/3 "2020-11-11T21:23:56Z")

</div>

Hi,  
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!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 November 2020 22:38 UTC](https://discourse.nodered.org/t/any-data-structure-with-key-value-but-key-not-unique/35766/4 "2020-11-11T22:38:50Z")

</div>

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:

```auto
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]
map[key].push(value);

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 November 2020 22:38 UTC](https://discourse.nodered.org/t/any-data-structure-with-key-value-but-key-not-unique/35766/5 "2020-11-25T22:38:58Z")

</div>

Closed on 25 November 2020 22:38 UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
