I have a form where a set of fields is repeated a couple of times. This form is then send as FormData (https://developer.mozilla.org/en-US/docs/Web/API/FormData) to the backend.
My current solution is to name the fields in the form like this:
<input name="firstname" />
<input name="lastname" />
<input name="firstname" />
<input name="lastname" />
Then on the backed I can convert FormData to an array of objects like this:
const firstnames = formData.getAll('firstnames')
const lastnames = formData.getAll('lastnames')
const persons = []
for (let i = 0; i < firstnames.length; i++) {
persons.push({
firstname: firstnames[i],
lastname: lastnames[i]
})
}
Since this seems to be a pretty standard scenario, I am just guessing that I am not on the right track to convert form data to a list of objects like this. How can this be done?