“JS Object to QueryString” Kode Jawaban

JS Object to QueryString

const params = {foo:"Hi", bar:"there"}
const queryString = new URLSearchParams(params).toString();

console.log(queryString);
Obedient Octopus

objek untuk menanyakan string js

const obj = {foo: "hi there", bar: "100%" };
const params = new URLSearchParams(obj).toString();
Panicky Panther

JavaScript mengonversi objek ke querystring

 const queryString = new URLSearchParams(props.location.search);
//or
var queryString = Object.keys(params).map(key => key + '=' + params[key]).join('&');
//Extracting query String key value pairs
for (let param of queryString.entries()) {
      console.log(param);
}
Rey

Obyek JavaScript untuk meminta string

function objectToQueryString(obj) {
  var str = [];
  for (var p in obj)
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  return str.join("&");
}

var person = { first_name : "Marty",last_name : "Mcfly"};
var queryString=objectToQueryString(person); //"first_name=Marty&last_name=Mcfly"
Grepper

Obyek JavaScript untuk meminta 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]
  }
})
);
Hiren Reshamwala

Jawaban yang mirip dengan “JS Object to QueryString”

Pertanyaan yang mirip dengan “JS Object to QueryString”

Lebih banyak jawaban terkait untuk “JS Object to QueryString” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya