When parsing with `comma: true`, params with a single or no value return a string rather than as an array. Is there an option to always return an array?
See original GitHub issueI looked at the docs and issues but I am not sure if there is an option built into qs that could do this.
Given a query string like:
?a=1&b=2,3&c=
When parsing with comma: true
, the result is:
{
a: "1",
b: ["2", "3"]
c: ""
}
What I want is a
and c
to be an array format instead. Is there an option to always return an array when parsing?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How can I convert a comma-separated string to an array?
If you intend to use the elements of your array as integers and not as strings after splitting the string, consider converting them...
Read more >Trailing commas - JavaScript - MDN Web Docs
JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item.
Read more >Parameter Serialization - Swagger
OpenAPI 3.0 supports arrays and objects in operation parameters (path, ... Header parameters always use the simple style, that is, comma-separated values.
Read more >Parsing arguments and building values — Python 3.11.1 ...
The keywords argument is a NULL -terminated array of keyword parameter names. Empty names denote positional-only parameters. Returns true on success; on failure ......
Read more >ARRAYTOTEXT function - Microsoft Support
Syntax · array. The array to return as text. Required. · format. The format of the returned data. Optional. It can be one...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In my use case, yes, I want everything as an array because, using the query example I mentioned,
a
andc
do take comma-separated values. Therefore, having keys which are all normalized to the same type -string[]
- makes data manipulation ib the Javascript side much easier. All of this, of course, with the necessity that none of the params are suffixed with[]
.Ultimately, what I was hoping was that qs had a built-in option to simply convert each one to a string array so that I don’t have to do the normalizing myself. Given your answer, I think it makes sense why qs wouldn’t have a built-in method for such.
You can do
[].concat(arrayOrString)
whenever you need to ensure you have an array, but yes i think you need to do that normalization yourself.