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

135
Views
How to form This JSON from this Javascript array of arrays?

I have this JavaScript array of arrays

   arr1= [ 
       ['a', 'b'],
       ['1', '2']
       ]
           

And I need to form this JSON object

[
    {
        "label":"a",
        "value":"1"
    },
    {
        "label":"b",
        "value":"2"
    },
]
  

How do I do it?

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

0

Using Array#map:

const arr = [ ['a', 'b'], ['1', '2'] ];

const [labels, values] = arr;
const res = labels.map((label, index) => ({ label, value: values[index] }));

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Look at this ...

const entries = new Map([
  ['foo', 'bar'],
  ['baz', 42]
]);

const obj = Object.fromEntries(entries);

console.log(obj);
// expected output: Object { foo: "bar", baz: 42 }

Did you mean something like this? Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries

about 4 years ago · Juan Pablo Isaza Report

0

You could take an array with the keys of the wanted objects and reduce and map the items.

const
    data = [['a', 'b'], ['1', '2']],
    keys = ['label', 'id'],
    result = data.reduce((r, a, i) => a.map((v, j) => ({ ...r[j], [keys[i]]: v })), []);
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!