javascript - Output array as comma separated with querySelector - Stack Overflow

I am using querySelector fromand I want to output an array as as ma separated string.I start with a U

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742282317a4414715.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信