Last active
September 28, 2022 07:19
-
-
Save TravelingTechGuy/32454b95a50d296912b9 to your computer and use it in GitHub Desktop.
JSON object to query string (using underscore/lodash)
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 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; | |
}; |
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
Here's some other options that don't require lodash.
From https://gist.github.com/3ch01c/37b7a292c744628f189eeabc917a309b/