Created
December 23, 2018 16:29
-
-
Save chrdek/9019c22d7273c2f44c111491c4a34986 to your computer and use it in GitHub Desktop.
String of value pairs to object array, encoded.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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