I've been trying to get a nested object from formData from a form with nested keys like the below:
<input name="item[0][id]" value="0121"/>
<input name="item[0][name]" value="Birmingham"/>
<input name="item[1][id]" value="01675"/>
<input name="item[1][name]" value="Warwickshire"/>
To be formatted as an object like this:
{
'item': [
{
'id': '0121',
'name': 'Birmingham'
},
{
'id': '01675',
'name': 'Warwickshire'
}
]
}
Not:
{
'item[0][id]': '0121',
'item[0][name]': 'Birmingham',
'item[1][id]': '01675',
'item[1][name]': 'Warwickshire'
}
Currently I'm using the below, which is outputting in the format above.
const formData = new FormData(this.form);
const object = Object.fromEntries(formData);
Essentially, I'd like to have the form data formatted as it would be when it gets received by PHP for example.