1const params = {lat: 35.681236, lng: 139.767125, zoom: 15};
2new URLSearchParams(params).toString();
3// "lat=35.681236&lng=139.767125&zoom=15"
4
5// OR
6
7const queryString = Object.keys(params).map(key => {
8 encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
9}).join('&');
1const queryString = Object.keys(params).map(key => {
2 encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
3}).join('&');
1function objectToQueryString(obj) {
2 var str = [];
3 for (var p in obj)
4 if (obj.hasOwnProperty(p)) {
5 str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
6 }
7 return str.join("&");
8}
9
10var person = { first_name : "Marty",last_name : "Mcfly"};
11var queryString=objectToQueryString(person); //"first_name=Marty&last_name=Mcfly"