solvit.io

Transform a JavaScript array or object into a URL query string without using jQuery

Solution
function param(object)
{
    var parameters = [];
    for (var property in object) {
        if (object.hasOwnProperty(property)) {
            parameters.push(encodeURI(property + '=' + object[property]));
        }
    }

    return parameters.join('&');
}
Example
function param(object) {
    // ...
}

var parameters = { 'user': 'John Smith', 'max': 10, 'sort': 'desc' };
var queryString = param(parameters);
// queryString = 'user=John%20Smith&max=10&sort=desc'

// equivalent code in jQuery:
// var queryString = $.param(parameters);
Find more solutions about: , ,