Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
php's http_build_query() in javascript
var build_query = function (obj, num_prefix, temp_key) {
var output_string = []
Object.keys(obj).forEach(function (val) {
var key = val;
num_prefix && !isNaN(key) ? key = num_prefix + key : ''
var key = encodeURIComponent(key.replace(/[!'()*]/g, escape));
temp_key ? key = temp_key + '[' + key + ']' : ''
if (typeof obj[val] === 'object') {
var query = build_query(obj[val], null, key)
output_string.push(query)
}
else {
var value = encodeURIComponent(obj[val].replace(/[!'()*]/g, escape));
output_string.push(key + '=' + value)
}
})
return output_string.join('&')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.