StringifyUrl with query-string creates ?type=fulltime&type=partime but i want ?type=fulltime.partime

382 Views Asked by At

I am using queryString with react to mutate some urls for api calls.

I have next code

useEffect(() => {
        const parsed = queryString.parseUrl(window.location.href);
        console.log('parsed',parsed)
        console.log(parsed.query.type=[parsed.query.type,'partime'])
        parsed.query.type = []
        if (fulltime) {
            parsed.query.type = [...parsed.query.type, 'fulltime'];
        }
        if (parttime) {
            parsed.query.type = [...parsed.query.type, 'parttime'];
        }

         setHref(queryString.stringifyUrl(parsed));
}
)

Problem is, queryString.stringifyUrl(parsed) //returns /?type=fulltime?type=parttime but i want /?type=fulltime,parttime

1

There are 1 best solutions below

0
On

while I was typing this answer in frustration, as usual I found solution myself.

queryString.stringifyUrl(parsed,  {arrayFormat: 'comma'})

we can add any type of option from arguments we are using in "parsing" of a url to the stringifyUrl\Stringify