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 create array of objects from array: javascript

I am trying to convert an array of elements to an array of objects in Javascript (react)
Here is the data I am getting from my API

"versions": [
    "1.0.1.2",
    "1.0.22.0",
    "1.1.0.12",
    "2.5.2.6",
    "2.5.2.7",
    "2.7.5.11",
    "2.7.7.7",
    "3.9.2.94",
    "3.9.3",
    "5.2.0.87",
    "9.5.0.210" ]

And I am trying to convert to an array of object which should look like this

options = [
    { value: "1.0.1.2", label: "1.0.1.2" },
    { value: "1.0.22.0", label: "1.0.22.0" },
    { value: "2.5.2.6", label: "2.5.2.6" },
];

I tried using the map function

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

versions = VersionloginData.data.versions.map((version) => [version.value, version.label])

But didn’t work out well , i am getting undefined as value objects

>Solution :

You needed to return an object inside the map callback:

versions = VersionloginData.data.versions.map((version) => ({ value: version, label: version }))
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