I'm trying to render a dynamic <select> element generated by React: the input-select component (see below), takes an array of objects and every object must contain the value for the <option> and its caption.
export default function InputSelect (props) {
return (
<label className="form__element">
{props.caption}
<select className="input-select">
{props.items.map((item, i) => <option key={i} value={item.value}>{item.caption}</option>)}
</select>
</label>
)
}
This becomes a problem when I try to pass an array of objects like this one:
[
{code: "IT", name: "Italy"},
{code: "US", name: "United States"},
]
where I have different keys which i cannot change since this data is retrieved from db.
How can i fix this?
<option key={item.code} value={item.code}>{item.value}</option>not work? Note: i've just used the property names from the objects in your sample array for purposes of illustration.