form element array keys in node and express form element array keys in node and express express express

form element array keys in node and express


It seems like the body parser uses an array (which must start at index 0) when it sees only numeric keys, and an object when there is at least one key that is non-numeric. With that in mind, you could try one of the following methods:

  1. Use a hidden form input to force the use of an object over an array. Example:

    <input type='hidden' name='block_payout[null]' /><input type='text' name='block_payout[14]' />...

    Results in the following body:

    { block_payout: { '14': 'test1', '15': 'test2', '16': 'test3', null: '' } }
  2. Prefix your keys with a non-numeric character to force the object mapping. Example:

    <input type='text' name='block_payout[i14]' />...

    Results in the following body:

    { block_payout: { i14: 'test1', i15: 'test2', i16: 'test3' } }