0

how can i convert elements in array as objects in array?

"allowed_vehicle_types": [
        "A1",
        "A2",
        "A",
        "B1",
]

How can I get this array into this shape? this is what i want

[
    { value: 'A1', label: 'A1' },
    { value: 'A2', label: 'A2' },
    { value: 'A', label: 'A' },
    { value: 'B1', label: 'B1' },
]
Programmer
  • 39
  • 5

1 Answers1

0

Using map on the array and return an object with value and label properties.

const data =  [
        "A1",
        "A2",
        "A",
        "B1",
]

const result = data.map(i => ({ value: i, label: i }));

console.log(result)
Mina
  • 14,386
  • 3
  • 13
  • 26