How to convert URL parameters to a JavaScript object?

前端 未结 30 1522
时光取名叫无心
时光取名叫无心 2020-11-22 13:57

I have a string like this:

abc=foo&def=%5Basf%5D&xyz=5

How can I convert it into a JavaScript object like this?

{
          


        
30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 14:19

    If you are using URI.js, you can use:

    https://medialize.github.io/URI.js/docs.html#static-parseQuery

    var result = URI.parseQuery("?foo=bar&hello=world&hello=mars&bam=&yup");
    result === {
      foo: "bar",
      hello: ["world", "mars"],
      bam: "",
      yup: null
    };
    

提交回复
热议问题