admin管理员组

文章数量:1331538

I am using querySelector from and I want to output an array as as ma separated string.

I start with a URL search string and then parse it using qs I then tried the qs stringify method to return the formatted string.

const sUrl = 'a=1&b=1&c=1&c=2&c=3';
const oData = qs.parse(sUrl);
// oData returns: 
{
  a: 1,
  b: 1,
  c: ['1', '2', '3']
}
const sData = qs.stringify(oData);
// sUrl returns: 'a=1&b=1&c%5B0%5D=1&c%5B1%5D=2&c%5B2%5D=3'

I want the output to be: a=1&b=1&c=1,2,3

I am using querySelector from https://www.npmjs./package/qs and I want to output an array as as ma separated string.

I start with a URL search string and then parse it using qs I then tried the qs stringify method to return the formatted string.

const sUrl = 'a=1&b=1&c=1&c=2&c=3';
const oData = qs.parse(sUrl);
// oData returns: 
{
  a: 1,
  b: 1,
  c: ['1', '2', '3']
}
const sData = qs.stringify(oData);
// sUrl returns: 'a=1&b=1&c%5B0%5D=1&c%5B1%5D=2&c%5B2%5D=3'

I want the output to be: a=1&b=1&c=1,2,3

Share Improve this question asked May 10, 2019 at 0:20 UXCODAUXCODA 1,2462 gold badges22 silver badges34 bronze badges 1
  • I found a second argument arrayFormat: ma but even that did not work. I expect because it's an array inside an object. – UXCODA Commented May 10, 2019 at 0:38
Add a ment  | 

1 Answer 1

Reset to default 8

qs has an option to specify the array format, so to get the desired output, you can use:

qs.stringify(oData, { arrayFormat: 'ma', encode: false  })

encode: false is also used so the mas aren't URL encoded.

With an input of:

{
  a: 1,
  b: 1,
  c: ['1', '2', '3']
}

It will return:

a=1&b=1&c=1,2,3

本文标签: javascriptOutput array as comma separated with querySelectorStack Overflow