Created
May 1, 2018 09:45
-
-
Save JohnMica/5e1334ebba6231c592a8d70c5456f268 to your computer and use it in GitHub Desktop.
serialize object with array of objects
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
var serialize = function(obj, prefix) { | |
var str = [], | |
p; | |
for (p in obj) { | |
if (obj.hasOwnProperty(p)) { | |
var k = prefix ? prefix + "[" + p + "]" : p, | |
v = obj[p]; | |
str.push( | |
v !== null && typeof v === "object" | |
? serialize(v, k) | |
: encodeURIComponent(k) + "=" + encodeURIComponent(v) | |
); | |
} | |
} | |
return str.join("&"); | |
}; | |
var codedInfo = serialize(dataObj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment