Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Can I use the same map key several times?

I'm storing and retrieving data like this:

data = new Map();
data.set("key", id);
data.set("key", id2);
data.get("key");

the second set is overwriting key element. I want a structure, that can hold multiple times the key element or one key element with multiple id data pairs. Does such a collection exist in Javascript or how to achieve this?

thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

JS Map and no other JS structure I'm aware of allows you to have more occurences of the same key. Keys must be unique.

I think that maybe unshifting new values into an array stored in a regular object might be a good way to achieve what you want. That way, your newest value will always be the first in the array, and thus accessible at index 0, like so:

const data = {key: []};

data.key.unshift(id);
data.key[0] // value of id

data.key.unshift(id2);
data.key[0] // value of id2
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!