I've an Array with Objects in this format: [{…}, {…}] How can i append another Object to it?
Im getting data from mongodb like [{value: '1', label: 'Tomato'}, {value: '2', label: 'Cheese'}]
and i wanna add {value: '3', label: 'onion'} to it with react select
const [defaultToppings, setDefaultToppings] = useState([]);
const [selectedToppings, setSelectedToppings] = useState([null]);
finalToppings = [...defaultToppings, ...selectedToppings] // Idk about this part
const handleSubmit = async () => {
try {
await axios.patch(
`/api/products/${editProduct._id}`,
{
selectedToppings,
},
{
headers: { Authorization: token },
}
);
setSuccess(true);
} catch (err) {
err.response.data.msg && setErr(err.response.data.msg);
}
};
<Select
key={defaultToppings}
options={extraOptions}
isMulti
defaultValue={defaultToppings}
onChange={setSelectedToppings}
components={animatedComponents}
/>