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('&');
1var queryString = Object.keys(params).map(key => key + '=' + params[key]).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"
1 const queryString = new URLSearchParams(props.location.search);
2//or
3var queryString = Object.keys(params).map(key => key + '=' + params[key]).join('&');
4//Extracting query String key value pairs
5for (let param of queryString.entries()) {
6 console.log(param);
7}
1const obj = {foo: "hi there", bar: "100%" };
2const params = new URLSearchParams(obj).toString();
1queryBuilder = function(obj, prefix) {
2 var str = [],
3 p;
4 for (p in obj) {
5 if (obj.hasOwnProperty(p)) {
6 var k = prefix ? prefix + "[" + p + "]" : p,
7 v = obj[p];
8 str.push((v !== null && typeof v === "object") ?
9 serialize(v, k) :
10 encodeURIComponent(k) + "=" + encodeURIComponent(v));
11 }
12 }
13 return str.join("&");
14}
15
16console.log(
17serialize({
18 foo: "hi there",
19 bar: {
20 blah: 123,
21 quux: [1, 2, 3]
22 }
23})
24);