Skip to content

Instantly share code, notes, and snippets.

@jonmaim
Created December 8, 2012 10:51
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonmaim/4239779 to your computer and use it in GitHub Desktop.
Save jonmaim/4239779 to your computer and use it in GitHub Desktop.
Generate query string for angular.js
/* Converts an object into a key/value par with an optional prefix. Used for converting objects to a query string */
var qs = function(obj, prefix){
var str = [];
for (var p in obj) {
var k = prefix ? prefix + "[" + p + "]" : p,
v = obj[k];
str.push(angular.isObject(v) ? qs(v, k) : (k) + "=" + encodeURIComponent(v));
}
return str.join("&");
}
@jaf0
Copy link

jaf0 commented Aug 1, 2014

qs({foo: 'bar', UTM: {a: 'b', c: 'd'}})
"foo=bar&UTM[a]=undefined&UTM[c]=undefined"

@jaf0
Copy link

jaf0 commented Aug 1, 2014

@SmithWebster
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment