Implement a function that converts a given JSON object to a query string.
HardProblem Statement
Implement a function that converts a given JSON object to a query string.
Sample Input
{
name: "John Doe",
age: 30,
city: "New York" }Sample Output
name=John%20Doe&age=30&city=New%20York
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
function toQueryString(obj) {
// Map object keys and values to key=value pairs
// Use encodeURIComponent to handle special characters
}
const params = { name: "John Doe", age: 30, city: "New York" };
console.log(toQueryString(params));
Input
{
name: "John Doe",
age: 30,
city: "New York" }Expected Output
name=John%20Doe&age=30&city=New%20York