Skip to content

How to convert object data to dropdown options?

I have created two dropdowns and I am getting the back-end data. Here, I wat to display the list of websites and stores as dropdown.

[
  {
    store: [],
    website: "storeuiteam",
  },
  {
    store: ["MSP_BASE"],
    website: "base",
  },
]

I want option like:

const tableDropdownOptions = [
  { value: "website1", label: "website1" },
  { value: "website2", label: "website2" },
];

Answer

Is that what you need? This pretty much is the same as your comment without the count.

const response = [{
  "store": [],
  "website": "storeuiteam"
}, {
  "store": ["MSP_BASE"],
  "website": "base"
}]

const options = response.map(item => ({ key: item.website, value: item.website }));

console.log(options)

EDIT

Here is a snippet that returns exactly the expected output

const response = [{
  "store": [],
  "website": "storeuiteam"
}, {
  "store": ["MSP_BASE"],
  "website": "base"
}]

const options = response.map((_, index) => ({ key: `website${index + 1}`, value: `website${index + 1}` }));

console.log(options)