1

In express, when I do a post from a form:

<tr>
  <td><input type="text" name="name"></td>
  <td><input type="text" name="remarks"></td>
</tr>
<tr>
  <td><input type="text" name="name"></td>
  <td><input type="text" name="remarks"></td>
</tr>

var body = req.body;, body give me this:

{ name: [ 'Tel', 'Tel2' ], remarks ['test1','test2'] }

How can I get this in json array:

{debtor: [{name:'Tel', remarks:'test1'}, {name:'Tel2', remarks:'test2'} ]

If the remarks test1 is empty, I wont be able to know test2 is belong to row 1 or row2.

2
  • What is your current code? Commented Dec 27, 2014 at 8:05
  • @dfsq var body = req.body; Commented Dec 27, 2014 at 8:08

1 Answer 1

1

Just transform the req.body object as you wish.

var body = [];
for(var n=0; n<req.body.name.length; n++){
  body.push({name : req.body.name[n], remarks : req.body.remarks[n]});
}
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is when the first row remarks might be empty, so from the code, it won't be able to identity the remarks is from first or second row.
make it an empty string on validation when the user doesn't fill the remarks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.