DekGenius.com
JAVASCRIPT
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('&');
object to query string javascript
const queryString = Object.keys(params).map(key => {
encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
}).join('&');
object to query string js
const obj = {foo: "hi there", bar: "100%" };
const params = new URLSearchParams(obj).toString();
query string to object javascript
Object.fromEntries(new URLSearchParams(query.filter))
js create query string from object
new URLSearchParams(object).toString()
query string to object javascript
function paramsToObject(entries) {
const result = {}
for(const [key, value] of entries) { // each 'entry' is a [key, value] tupple
result[key] = value;
}
return result;
}
query string to object javascript
var search = location.search.substring(1);
JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) })
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]
}
})
);
© 2022 Copyright:
DekGenius.com