In my scenario, I have a set of fixed options along with a dynamic number of yes/no radio inputs named other[index]
. By utilizing $(form).serializeArray()
, I can obtain an array of name/value objects. Through the use of the reduce method, I am then able to condense them into a single object.
const serializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = serializedForm.reduce((aggregation, option) => {
return {
...aggregation,
[option.name]: option.value === 'true'
}
}, {});
The issue lies in the result not aligning with my desired outcome:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}
What I actually desire is:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}
Do you have any recommendations on achieving this structure?