In Node.js and Express I'm creating a poll app with questions and responses. I'm writing a route to get data from a series of sqlite tables. I want to convert this flat table of data into a nested object array.
I use db.all to get a table of data that looks like:
pollId | question | optionId | option | responseId | responseName
---------------------------------------------------------------------------
1 | Question text | 1 | Answer 1 | 18 | Joe Bloggs
1 | Question text | 1 | Answer 1 | 19 | Jane Doe
1 | Question text | 2 | Answer 2 | 15 | Dave Black
2 | Second question | 1 | Yes | 20 | Susan Green
I want to convert this data into an object array that looks like:
[{
pollId: 1,
question: "Question text",
options: [
{optionId: 1, option: "Answer 1", responses:[
{responseId: 18, responseName: Joe Bloggs},
{responseId: 19, responseName: Jane Doe}
]},
{optionId: 2, option: "Answer 2", responses:[
{responseId: 15, responseName: Dave Black},
]}
]
},
{
pollId: 2,
question: "Second question",
options: [
{optionId: 1, option: "Yes", responses:[
{responseId: 18, responseName: Susan Green}
]}
]
}]
What is the best way to do this? Should I just be iterating down the rows? Or anything more sophisticated?