I am trying to convert values to query string.
var obj = {
param1: 'test',
param2: true,
}
var str = "";
for (var key in obj) {
if (str != "") {
str += "&";
}
str += key + "=" + obj[key];
}
console.log(str);
Expected query string would be like this: test=true.
>Solution :
It would be much easier if you would use objects as intended but:
const obj = {
param1: 'test',
param2: 'true',
param3: 'test2',
param4: 'NO',
}
const entries = Object.values(obj)
const trueObj = {}
for (let i = 0; i < entries.length; i += 2) {
trueObj[entries[i]] = entries[i + 1]
}
const params = new URLSearchParams(trueObj)
const queryString = params.toString()
console.log(queryString)