Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with non-zero-based names #27

Closed
lucaswxp opened this issue Aug 26, 2014 · 1 comment
Closed

Problems with non-zero-based names #27

lucaswxp opened this issue Aug 26, 2014 · 1 comment
Assignees
Labels
Milestone

Comments

@lucaswxp
Copy link

Heys guys.

When I have a input like:

Qs.parse('obj[0]=value&obj[1]=value')

I get the expected array value

{obj: ['value', 'value']};

But when the input doesn't start with a zero based index, for instance:

Qs.parse('obj[1]=value&obj[2]=value')

I would expect a return value such as:

{obj: {'1': 'value', '2': 'value'}};

But I still get the same result as before:

{obj: ['value', 'value']};

Reference: expressjs/body-parser#40

@nlf
Copy link
Collaborator

nlf commented Aug 26, 2014

That's because by default qs is creating a sparse array and placing the members in the correct indices, it then compacts that array preserving order, thus:

Qs.parse('obj[2]=value2&obj[1]=value1');

will yield

{ obj: ['value1', 'value2'] }

You can force creating an object by setting the arrayLimit option to 0:

Qs.parse('obj[1]=value&obj[2]=value', { arrayLimit: 0 });

which will give you:

{ obj: { '1': 'value', '2': 'value' } }

@nlf nlf added the question label Aug 26, 2014
@nlf nlf added this to the 2.1.1 milestone Aug 26, 2014
@nlf nlf self-assigned this Aug 26, 2014
@nlf nlf closed this as completed Aug 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants