Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Last active September 30, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/9d3e4c33f9265285969106744a873095 to your computer and use it in GitHub Desktop.
Save Ciantic/9d3e4c33f9265285969106744a873095 to your computer and use it in GitHub Desktop.
Parse url search parameters to object
Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {});
// {foo: ["1","2","3"], blah: ["a","b"]}
// Or directly to string
Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => a.has(k) && (a.set(k, a.get(k) + "," + v) ?? a) || (a.set(k, v) ?? a), new URLSearchParams()).toString();
// "foo=1%2C2%2C3&blah=a%2Cb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment