Last active
September 30, 2020 19:35
-
-
Save Ciantic/9d3e4c33f9265285969106744a873095 to your computer and use it in GitHub Desktop.
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