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 customize options in react-select?

I’m using react-select but I have a data in object not in array & in options only accepting array values. How can I solve it?

 <Select
   options={ccUsers}
   onChange={handleChange}
 />

Working data :-

  const ccUsers = [
    { value: 'One', label: 'One' },
    { value: 'Two', label: 'Two' },
  ];

But getting data in this format :-

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

const ccUsers = {
    "test": "testing",
    "test": "testing"
}

>Solution :

You can use Object.entries to convert into Array

const obj = {
  'One': 'One',
  'Two': 'Two',
};
const ccUsers = Object.entries(obj).map(([key, value]) => ({
  label: key,
  value
}))
console.log('>>>>>  ccUsers : ', ccUsers);
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