Skip to content

Instantly share code, notes, and snippets.

@chrdek
Created December 23, 2018 16:29
Show Gist options
  • Save chrdek/9019c22d7273c2f44c111491c4a34986 to your computer and use it in GitHub Desktop.
Save chrdek/9019c22d7273c2f44c111491c4a34986 to your computer and use it in GitHub Desktop.
String of value pairs to object array, encoded.
//Extract key-values from string in the format of 'k1=v1&k2=v2&k3=v3' (with uri decoding)
String.prototype.ToKvps = function(){
var qd={};
this.split`&`.forEach(item => {let [k,v] = item.split`=`; v = v && decodeURIComponent(v);
(qd[k] = qd[k] || []).push(v)});
return qd;
}
//Example: JSON.stringify("fd=44&ds=32&dks=1314%41&sd=39%209&ds=dsl%20ds".ToKvps());
//Output: "{"fd":["44"],"ds":["32","dsl ds"],"dks":["1314A"],"sd":["39 9"]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment