Parse url search parameters to object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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