arrayFormat equivalent in URLSearchParams

Viewed 690

Coming from qs we would control how arrays in query parameters would be with the arrayFormat option (quote from qs docs below):

You may use the arrayFormat option to specify the format of the output array:

qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
// 'a[0]=b&a[1]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
// 'a[]=b&a[]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
// 'a=b&a=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
// 'a=b,c'

How can we control the format with URLSearchParams?

Doing:

new URLSearchParams({ a: ['b', 'c']})

Always gives a=b,c.

1 Answers

You can't. That isn't a feature of URLSearchParams.

If you need that feature, then use qs instead.

Related