简体   繁体   中英

Output array as comma separated with querySelector

I am using querySelector from https://www.npmjs.com/package/qs and I want to output an array as as comma 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

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

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

encode: false is also used so the commas 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM