Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I’m asking this question again since I got downvotes due to unclear explanation in previous question:

I have this java script array of arrays

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

And I need to form this JSON object

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

How do i do it?

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading