I would have come up with something like this: Object.entries(Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {})).map(([key, values]) => `${key}=${values.join(",")}`).join("&");
JavaScript: Search and Don’t Replace (2008)
11–20 of 46 posts
Re: JavaScript: Search and Don’t Replace (2008)
#12https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re: JavaScript: Search and Don’t Replace (2008)
#13I would have come up with something like this: Object.entries(Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {})).map(([key, values]) => `${key}=${values.join(",")}`).join("&");
Re: JavaScript: Search and Don’t Replace (2008)
#14Just use match instead? Why use replace and hack into an array? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re: JavaScript: Search and Don’t Replace (2008)
#15> Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. Jamie Zawinski
https://stackoverflow.com/questions/1732348/regex-match-open...
Re: JavaScript: Search and Don’t Replace (2008)
#16> Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. Jamie Zawinski
Kipling
Re: JavaScript: Search and Don’t Replace (2008)
#17Just use match instead? Why use replace and hack into an array? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
I know it's weird, but that's what $.each existed for. for ... of did not exist.
Re: JavaScript: Search and Don’t Replace (2008)
#18Please give me an 80 kloc project full of this kind of smartness, preferably minefied.
Re: JavaScript: Search and Don’t Replace (2008)
#19I would have come up with something like this: Object.entries(Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {})).map(([key, values]) => `${key}=${values.join(",")}`).join("&");
If you read the article he makes a point about not using intermediate arrays and .join()
I was hoping other people to come up modern solutions to this same original problem.
Even my solution is questionable, because it relies on generating new URLSearchParams with strings, if one wants to be secure the reduce should take URLSearchParams as accumulator and add the items there.
Re: JavaScript: Search and Don’t Replace (2008)
#20I would have come up with something like this: Object.entries(Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {})).map(([key, values]) => `${key}=${values.join(",")}`).join("&");
If you read the article he makes a point about not using intermediate arrays and .join()
Edit: Woah, made a hash of that before going in and fixing all the typos from my phone keyboard.