qs icon indicating copy to clipboard operation
qs copied to clipboard

How can you keep the square brackets when using arrayFormat: comma

Open samgermain opened this issue 1 year ago • 4 comments

With the following code

qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
// 'a=b,c'

plus some option, I want to obtain a result that looks like

// 'a=[b,c]'

How can I do this?

samgermain avatar Sep 06 '23 01:09 samgermain

That’s not the comma format, so I’m not sure how you can.

What server expects that format??

ljharb avatar Sep 06 '23 02:09 ljharb

That’s not the comma format, so I’m not sure how you can.

well it doesn't need to be the comma format then, but it needs to be that format

What server expects that format??

binance

samgermain avatar Sep 06 '23 17:09 samgermain

oof, so binance basically just invented their own format instead of using the universal ones that already exist?

I'm not sure how to do that with qs; perhaps with a custom encoder?

ljharb avatar Sep 06 '23 17:09 ljharb

You can use filter.

qs.stringify(params, {
  filter: (prefix, value) => {
    if (Array.isArray(value)) return `[${value.join(',')}]`
    return value
  },
})

lzdyes avatar Nov 21 '23 06:11 lzdyes