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

300
Views
map: is there any way we can add keys to an array which is based on its values

I have the following Map:

'1st key' => [{'name': 'apple'}, {'other': 'orange'}],
'2nd key' => [{'name': 'mango'}, {'other': 'lemon'}]
    

I want to cenvert this map to a new array and also add keys.

Array.from(map.values()) converts values to array, es expected, but I want to add keys as attributes...

Expected result:

 [ {'id': '1st key'}, {'name': 'apple'}, {'other': 'orange'}]
 [ {'id': '2nd key'}, {'name': 'mango'}, {'other': 'lemon'}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use map.entries() so you get the keys and values. Then you can insert the key at the beginning of the value array.

Array.from(map.entries()).map(([key, val]) => [{id: key}, ...val]);
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!