Answers for "javascript how object to query string"

3

js query string from object

const params = {lat: 35.681236, lng: 139.767125, zoom: 15};
new URLSearchParams(params).toString();
// "lat=35.681236&lng=139.767125&zoom=15"

// OR

const queryString = Object.keys(params).map(key => {
  encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
}).join('&');
Posted by: Guest on May-21-2021
-1

object to query string js

const obj = {foo: "hi there", bar: "100%" };
const params = new URLSearchParams(obj).toString();
Posted by: Guest on August-14-2020
-1

javascript object to query string

queryBuilder = function(obj, prefix) {
  var str = [],
    p;
  for (p in obj) {
    if (obj.hasOwnProperty(p)) {
      var k = prefix ? prefix + "[" + p + "]" : p,
        v = obj[p];
      str.push((v !== null && typeof v === "object") ?
        serialize(v, k) :
        encodeURIComponent(k) + "=" + encodeURIComponent(v));
    }
  }
  return str.join("&");
}

console.log(
serialize({
  foo: "hi there",
  bar: {
    blah: 123,
    quux: [1, 2, 3]
  }
})
);
Posted by: Guest on September-04-2020

Code answers related to "javascript how object to query string"

Code answers related to "Javascript"

Browse Popular Code Answers by Language