Code Lab

URL parameters

EasyJSON and data5 min

Write a function parseQuery(query) that turns a URL parameter string such as 'a=b&c=d' into an object { a: 'b', c: 'd' }. Pairs are separated by &, and a key and its value are separated by =. The values stay strings.

Examples

parseQuery('page=2&sort=price')

Returns { page: '2', sort: 'price' }

parseQuery('')

Returns {}

An empty string gives an empty object.

Asked at Airbnb · Source

Loading the editor...
  1. InputparseQuery('a=b&c=d')
    Expected{ a: 'b', c: 'd' }
  2. InputparseQuery('page=2&sort=price')
    Expected{ page: '2', sort: 'price' }
  3. InputparseQuery('')
    Expected{}

Hidden tests run on Submit.