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

Convert an Array to an Array of Object

Need help for converting an array to array of object adding label.

For example I have an array:

data = ["data1", "data2", "data3"]

I want to convert and adding label.
The result expected:

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: "data1", value: "data1"},
   {label: "data2", value: "data2"},
   {label: "data3", value: "data3"}
]

>Solution :

Just using data.map(d => ({label:d,value:d})) can do it

let data = ["data1", "data2", "data3"]

let result = data.map(d => ({label:d,value:d}))

console.log(result)
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