CodeWars: Objectify A Url Query String Solution

1// Converts a URL Query String into an object map
2function convertQueryToMap(query) {
3 const res = {}
4 const keyValuePairs = query
5 .split("&")
6 .map(kvp => kvp.split("="))
7 .filter(([key]) => Boolean(key))
8 for (const keyValuePair of keyValuePairs) {
9 const [key, value] = keyValuePair
10 const path = key.split(".")
11 let acc = res
12 for (const [i, prop] of path.entries()) {
13 if (i === path.length - 1) {
14 acc[prop] = decodeURIComponent(value)
15 continue
16 }
17 if (!acc[prop]) {
18 acc[prop] = {}
19 }
20 acc = acc[prop]
21 }
22 }
23 return res
24}

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: Longest Common Subsequence (Performance Version)

HoningJS

Search Posts