CodeWars: Objectify A Url Query String Solution
1// Converts a URL Query String into an object map2function convertQueryToMap(query) {3 const res = {}4 const keyValuePairs = query5 .split("&")6 .map(kvp => kvp.split("="))7 .filter(([key]) => Boolean(key))8 for (const keyValuePair of keyValuePairs) {9 const [key, value] = keyValuePair10 const path = key.split(".")11 let acc = res12 for (const [i, prop] of path.entries()) {13 if (i === path.length - 1) {14 acc[prop] = decodeURIComponent(value)15 continue16 }17 if (!acc[prop]) {18 acc[prop] = {}19 }20 acc = acc[prop]21 }22 }23 return res24}
Comments
Loading comments...
Tags
codewars
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
CodeWars: Defuse The Bombs
Feb 9, 2021