I have a nested array of objects that looks something like this:
[
[
{
name: 'name',
prettyFormat: 'joe bloggs'
uselessInfo: 'drjfghsdr'
},
{
name: 'email',
answer: '[email protected]',
uselessInfo: 'liodrgjiwe'
}
]
[
{
name: 'name',
prettyFormat: 'mark obrien',
uselessInfo: 'drjfghsdr'
},
{
name: 'email',
answer: 'mark [email protected]',
uselessInfo: 'liodrgjiwe'
}
]
]
I need to transform this array into something more sensible, actually a JSON object, with the following format:
{
"LIST": [
{
"name": "joe bloggs",
"email": "[email protected]",
},
{
"name": "mark o'brien",
"email": "mark [email protected]",
},
]
}
This is my solution:
let outputBuilder = []
staffSubmissionsArray.forEach(subArray => {
let personObj = {}
subArray.forEach(obj => {
if (obj.name == 'name') {
personObj['name'] = obj.prettyFormat
} if (obj.name == 'email') {
personObj['email'] = obj.answer
}
outputBuilder.push(personObj)
})
})
console.log(JSON.stringify({ "LIST" : outputBuilder })
This gets everything into the right format, however, each person is getting duplicated, presumably each time I iterate over each object, but I don’t know how to fix this. Do I need to approach this in a different way?