Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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