/**
* Open up the console to see what's going on
*/
function parse(str) {
// parse will split the string along the &
// and loop over the result
var keyValues, i, len, el,
parts, key, value, result;
result = {};
sepToken = '&';
keyValues = str.split('&');
i = 0;
len = keyValues.length;
for(i; i<len; i++) {
el = keyValues[i];
parts = el.split('=');
key = parts[0];
value = parts[1];
// this will replace any duplicate param
// with the last value found
result[key] = value;
}
return result;
}
function serialize(data) {
// serialize simply loops over the data and constructs a new string
var prop, result, value;
result = [];
for(prop in data) {
if (data.hasOwnProperty(prop)) {
value = data[prop];
// push each seriialized key value into an array
result.push(prop + '=' + value);
}
}
// return the resulting array joined on &
return result.join("&");
}
function run() {
// run this example
var paramStr, data, result;
// paramStr has a duplicate param, value1
paramStr = 'value1=bla&value1=foop&value2=ble&page=test&value4=blo&test=blu&car=red';
data = parse(paramStr);
result = serialize(data);
return result;
}
console.log(run());
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.