Skip to content

Instantly share code, notes, and snippets.

@TravelingTechGuy
Last active September 28, 2022 07:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save TravelingTechGuy/32454b95a50d296912b9 to your computer and use it in GitHub Desktop.
Save TravelingTechGuy/32454b95a50d296912b9 to your computer and use it in GitHub Desktop.
JSON object to query string (using underscore/lodash)
var objectToQueryString = function(obj) {
var qs = _.reduce(obj, function(result, value, key) {
return (!_.isNull(value) && !_.isUndefined(value)) ? (result += key + '=' + value + '&') : result;
}, '').slice(0, -1);
return qs;
};
@giiska
Copy link

giiska commented Sep 28, 2022

Object.entries(someobject).reduce((result, item)=>{return result += ${item[0]}=${item[1]}&}, '').slice(0, -1)

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