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