Skip to content

Instantly share code, notes, and snippets.

@JohnMica
Created May 1, 2018 09:45
Show Gist options
  • Save JohnMica/5e1334ebba6231c592a8d70c5456f268 to your computer and use it in GitHub Desktop.
Save JohnMica/5e1334ebba6231c592a8d70c5456f268 to your computer and use it in GitHub Desktop.
serialize object with array of objects
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