JSFiddle - React, Tailwind, and code Playground

by Alexander

JavaScript

console.clear();

var q = "a[foo]=a&a[bar]=b&hotel_ids[]=1&hotel_ids[]=2&departure=491&hotel_ids%5B%5D=4966&date_begin=25.05.2017&date_end=28.05.2017&nights_min=6&nights_max=10&adults=2&children=0&arrival_id=188&arrival_type=country&time_started=1495528177673&session_hash=91178";

function parse(str) {
	var qo = {};
  decodeURIComponent(str)
  	.replace(/\+/g, ' ')
    .replace(
      new RegExp(`([^?=&]+)(=([^&]*))?`, 'g'),
      function($0, $1, $2, $3){
      	if (/\[.*?\]$/.test($1)) {
          var k = $1.match(/\[(.*?)\]/);
        	$1 = $1.replace(k[0],'');
        	if (!($1 in qo)) qo[$1] = k[1] ? {} : [];
        	qo[$1][ k[1] ? k[1] : qo[$1].length++ ] = $3;
        }
      	else qo[$1] = $3;
      }
  );
	return qo;
}

var a = parse(q);
console.log(a);


/*
const parse = str => decodeURIComponent(str)
  .split('&')
  .map(function(n){ return n = n.split("="), this[n[0]] = n[1], this }.bind({}))[0];
//*/

/*
const parse = str => decodeURIComponent(str).replace(/\+/g, ' ')
	.split('&')
	.map(v => v.split('=').map(v=>v.trim()))
//  .map(v => )
//  .reduce((a,[k,v]) => Object.assign(a, {[k]:v}), {});
//*/