If I want the Node.js query parser to parse an array, I can send this:
'?or=foo&or=bar' // gets me { or: ['foo', 'bar'] }
If I want an object I can do this:
'?or[foo]=bar' // gets me { or: {foo: 'bar'}}
But how do I get an array of objects? I'd like this output:
{ or: [{foo: 'bar'}, {bar: 'baz'}]}
We can also pass an array with tuples or a query string. With that done, we now have an instance of the URLSearchParams class. We can get the string version of this by calling toString and append this to our URL.
With the qs
module, you can get the object you're looking for if you use this format:
or[0][foo]=bar&or[1][bar]=baz
You can use options allowDots.
const stringParams = qs.stringify(params, {allowDots:true});
// myArray[0].name=MeWhit
qs.parse(stringParams , {allowDots: true});
// [{ name: MeWhit}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With