I have a url string like this "http://url.com/foo/aa=342&bb=66"
and I need to construct an object from it
{ aa: '342', bb: '66' }
Here is my attempt
function constructFromUrl(url) {
return url.split('/').at(-1).split('&').reduce((accu, curr) => {
const [key, value] = curr.split('=')
accu[key] = value
return accu
},{})
}
It works ok but I feel like it is really brittle. Is there any better way of handling this?
Also, I am really bad at naming things - is there a better name for such a function?