Skip to content

MostafaRostami72/http-build-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

http build query in javascript

php http_build_query i n javascript

function httpBuildQuery(object) {
    let query = '';

    Object.keys(object).forEach((key, index) => {
        const value = object[key];
        if (value && Array.isArray(value) && value.length) {
            let and_index = index;

            for (let item of value) {
                query += ((and_index > 0) ? '&' : '') + key + '[]=' + item;
                and_index += 1;
            }
        } else if (value && !Array.isArray(value) && typeof value !== "undefined" && value) {
            query += ((index > 0) ? '&' : '') + key + '=' + value;
        }
    });

    return query;
}


const obj = {
  a:a,
  b:b,
 }
 
 httpBuildQuery(obj) // output ?a=a&b=b

About

http build query in javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published