My formData serializeArray function returns an array of objects, such as:
[
0 : {name: 'animal_monkey', value: 'banana'}
1 : {name: 'animal_horse', value: 'radishes'}
2 : {name: 'fruit_banana', value: 'yellow'}
3 : {name: 'fruit_apple', value: 'red'}
]
I want to organize these elements into a single object based on categories with their values assigned, like this:
{
animal: {
monkey : banana,
horse : radishes
},
fruit: {
banana : yellow,
apple : red
}
}
I attempted this using reduce and Object assign but encountered issues where the values were being overwritten. Here's my attempt:
obj = keys.map((k, i) => k.reduceRight((value, key) => ({[key]: value}), vals[i]) )
result = Object.assign({}, ...obj)
The initial values don't survive the assignment process. Any suggestions on how to achieve the desired outcome?
Thank you!