0

I have this clever little piece of vanilla Javascript that converts url params into a hash:

"a=a&b=b".split("&").reduce((function(params, param) {
  var param_split = param.split("=").map(function(value) {
    return decodeURIComponent(value.replace("+", " "));
  });
  params[param_split[0]] = param_split[1];
  return params;
}), {});

Which outputs this hash:

{
  a: "a",
  b: "b"
}

But sometimes I get url params that are nested like this:

"a=a&b%5Bc%5D=bc&b%5Bd%5D=bd"

My script would output:

{
  a: "a",
  b[c]: "bc",
  b[d]: "bd"
}

What should I do in order to be able to output a nested hash like this?:

{
  a: "a",
  b: {
    c: "bc",
    d: "bd"
  }
}
4

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.