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 :-
const ccUsers = {
"test": "testing",
"test": "testing"
}
2
Answers
You can use
Object.entries
to convert into ArrayIt accepts an array of
{value: string, label:string}
like many other Select components, such as antd Select. You need to transform your data before sending it as a prop inside Select. Since you have key-value pairs in an object already, you can useObject.entries()
to do that. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries