I've got a series of radio button groups set up, to represent answers to questions.
In the format of:
What Is your favourite Colour Red | Blue
Markup:
<input type="radio" name="Question_1" value="Red" id="Question_1_Red" />
<label for="Question_1_Red">Red</label>
<input type="radio" name="Question_1" value="Blue" id="Question_1_Blue" />
<label for="Question_1_Blue">Blue</label>
I'm using node (express)
In req.body, I'm getting back the following:
Question_1: 'Red', Question_2: 'Other Answer', Question_3: 'Another Answer'
My question is, how can I put this into an array, so I can store it like:
[{questionID: 1, answer: "Red"}, {questionID: 2, answer: "Other Answer"}]
var newBody = [];
for (var k in req.body) {
var m = k.match(/^Question_(\d)$/);
if (m) {
newBody.push({questionID: m[1], answer: req.body[k]});
}
}