So im generating a form for users of my application to fill out from an array of objects, each object has its own validation data....etc. This issue is when the user fills out the form and a new object is created from it to send to the back end, if the user fills out the form in order, the object is in the proper order, but if the user goes back and changes a value, the object is put out of order.
Original Object:
const formData = [
{
name: 'One',
validation: 'Number',
},
{
name: 'Two',
validation: 'Number',
},
{
name: 'Three',
validation: 'Number',
},
{
name: 'Four',
validation: 'Number',
}
The new Object is built with a handle change method
Object Handle:
const [newForm, setNewForm] = useState({})
const handleChange = (e) => {
const { name, value } = e.target
setNewForm({ ...newForm, [name]: parseFloat(Number(value).toFixed(2)) })
}
Correct Object:
newForm = {
One: 1,
Two: 2,
Three: 3,
Four: 4,
}
Incorrect Object:
newForm = {
Two: 2,
One: 1,
Four: 4,
Three: 3,
}
I'm having a hard time trying to figure out a way of comparing the new form against the formData to make sure they stay in the proper order. If anyone could give some advice and help me out I'd appreciate it.