I have an object with filter value I want to form a query params based on the object, pls help
object = {
emp_id: 1,
firstname: abc,
lastname: xyz,
mobile: 12345,
email: [email protected]
}
url = emp_id=1&firstname=abc&lastname=xyz&mobile=12345&[email protected]
for (const [key, value] of Object.entries(object)) {
url = `${key}= ${value}&`;
}
here im getting only the last value
.map()then.join()on the array instead. (also please look for existing answers before posting a question)URLSearchParamsobject, which you can construct using the result fromObject.entriesdirectly:const params = new URLSearchParams(Object.entries(object));Then if you need a query string, useconst str = params.toString();urlon each loop iteration, not adding to it (you have=, not+=). Also note that you need to URI-encode the keys and values, you can't just use them directly.