I've got a form in React, which I need to convert to a different data structure.
The form has the data structure as below -
const [form, useForm] = useState([
{
id: 1,
title: "Section",
url: "testLink",
content: [{ id: 3, title: "", url: "" }]
},
{
id: 2,
title: "Section",
url: "testLink",
content: [{ id: 4, title: "", url: "" }]
}
]);
When the user hits submit, I need to delete id
property as this is not accepted from the backend.
I'm trying to do this in the following example -
const handleSubmit = (event) => {
const formData = [...form];
formData.map((item) => {
delete item.id;
});
console.log(form);
console.log(formData);
};
I'm expecting the form and the formData objects to have different properties (formData wouldn't have the id, but form would).
I'm not sure what I'm doing wrong here? Here's a demo from CodeSandbox (I did slightly modify this to get it working asap) - https://codesandbox.io/s/elated-noether-xcr6r?file=/src/App.js
Thanks for taking the time to look into this!