0

After decode GET parameters:

var query = decodeURIComponent(document.location.search)

I get:

a[0]=data&a[1][one]=data&a[1][two]=data&b=data

I need to convert it into an object like this:

myObject = {
    a : {
          0 : data,
          1 : {
              one : data,
              two: data
          }
    },
    b : data
}

I don't know how to do it. I am blank.

EDIT. This problem is quite different from possible duplicates. Here I have an multidimensional array. I could split that very easily into this:

myObject = {
   "a[0]" : data,
   "a[1][one]" : data,
   //etc
}

but I don't need that.

Angel
  • 674
  • 8
  • 15
  • Possible duplicate of: http://stackoverflow.com/questions/4297765/make-a-javascript-array-from-url or http://stackoverflow.com/questions/8559075/get-array-value-from-get-method See also http://quickhow.net/2012/code/how-to-convert-get-url-structure-to-array-and-reverse – Domino Aug 24 '15 at 15:25
  • 1
    possible duplicate of [Convert URL parameters to a javascript object](http://stackoverflow.com/questions/8648892/convert-url-parameters-to-a-javascript-object) – michelem Aug 24 '15 at 15:26
  • Is `data` an identifier, or a string? – Amit Aug 24 '15 at 15:27
  • @Amit, data is a string – Angel Aug 24 '15 at 15:29
  • I think that input you are noting is not accurate for the expected new structure. I believe you have a `2` in the array where it should still be a `1` like such: `a[0]=data&a[1][one]=data&a[1][two]=data&b=data` – Kenneth Salomon Aug 24 '15 at 15:49
  • @Amit, my problem —I think— is quite different – Angel Aug 24 '15 at 16:51
  • @KennethSalomon, Thanks, I've edited the error. – Angel Aug 24 '15 at 16:52
  • I guess you're right, it does look a little different. Can you control the transport method of your object? If you could use json (as a request body, or a stringified query string) it would be much easier for you – Amit Aug 24 '15 at 18:37

1 Answers1

0

I figured out a temporary solution, surely an awful and not proper one. Till I have a better one, I will use this. I think It may be useful to someone.

Next, I share a version of a till two level dimensional array. But it can be adapted to a deeper one:

function getParams() {
    var query = decodeURIComponent(document.location.search.substring(1));
    var params = {},
        tokens,
        r = /[?&]?([^=]+)=([^&]*)/g;

    var key, value, match = [],
        re = /\[([^\]]*)\]/g;

    while (tokens = r.exec(query)) {
        value = tokens[2];
        key = tokens[1].split('[');
        key = key[0];
        match = [];
        while ( (  res = re.exec(tokens[1]) ) != null ){
           match.push(res[1])
        }

        if (match) {
            switch (match.length) {
                case 1 :
                    params[key] = params[key] || {};
                    params[key][match[0]] = value;
                    break;
                case 2 :
                    params[key] = params[key] || {};
                    params[key][match[0]] = params[key][match[0]] || {};
                    params[key][match[0]][match[1]] = value;
                    break;
            }
        } else {
            params[key] = value;
        }
    }

    return params;
}
Angel
  • 674
  • 8
  • 15