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

282
Views
Creating an array from 1 keys array and 1 values array in JavaScript?

I have two arrays that look like this:

    ["Test", "Test2", "Test3"]
    ["ID1", "ID2", "ID3"]

I need to create an array from the above arrays that looks like this:

    ["Test": "ID1", "Test2": "ID2", "Test3": "ID3"]

I'm currently doing this but this is not correct:


    var new_arr = [];
    var something = 'Test';
    var something2 = 'ID1';
    
    var n = ''+something+':'+something2+'';
    
    new_arr.push(n);

This code produces the following which is not right:

    ["Test:ID1","Test2:ID2"]

Could someone please advise on this?

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

0

What you want seems to be a better fit for a plain object, not an array.

For that you can use map and Object.fromEntries:

let keys = ["Test", "Test2", "Test3"];
let values = ["ID1", "ID2", "ID3"];

let obj = Object.fromEntries(keys.map((key, i) => [key, values[i]]));

console.log(obj);

Or if you need separate objects, each with just one key/value pair, then use a computed property name in an object literal:

let keys = ["Test", "Test2", "Test3"];
let values = ["ID1", "ID2", "ID3"];

let arr = keys.map((key, i) => ({ [key]: values[i] }));

console.log(arr);

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!