how to convert {id: 111, name: "aaa"}
to "id=111&name=aaa"
how to convert {id: 111, name: "aaa"}
to "id=111&name=aaa"
function toUrlSearch (obj) {
return new URLSearchParams(obj).toString()
}
it is most convenient to use qs
module
https://github.com/ljharb/qs
the querystring; front end of the module that comes with nodejs should have a similar library
var querystring = require('querystring');
var obj = { id: 111, name: 'aaa'};
console.log(querystring.stringify(obj)); // "id=111&name=aaa"
version 1.0
function jsonToQuery(json) {
let result = [];
for (let key in json) {
result.push(key + '=' + json[key]);
}
return result.join('&');
};
version 2.0
function jsonToQuery(json, replace){
let result = [];
replace = replace || function(value){
return value;
}
for(let key in json){
let item = json[key];
result.push(key + '=' + replace(json[key]));
}
return result.join('&');
}
/**
* object
* @param obj
* @returns {boolean}
*/
const isPlainObject = obj => {
if (typeof obj !== 'object' || obj === null) return false;
let proto = obj;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
return Object.getPrototypeOf(obj) === proto;
};
/**
* objectarray
* @param obj
*/
const isObjectOrArray = obj =>
obj !== null && (Array.isArray(obj) || isPlainObject(obj));
/**
* dataquery string
* @param data
* @returns {string}
*/
const dataToQueryString = (data = null) => {
let queryString = '';
if (data !== null) {
const propsArray = Object.keys(data);
const queryArray = [];
propsArray.forEach(props => {
const value = data[props];
if (value !== undefined && value !== '') {
if (isObjectOrArray(value)) {
queryArray.push(`${props}=${encodeURI(JSON.stringify(value))}`);
} else {
queryArray.push(`${props}=${value}`);
}
}
});
queryString = queryArray.join('&');
}
return queryString;
};
< H2 > how did the printed index max item come from < H2 >? ...
there is an object var obj = { a: { b: { c: 3 } } }; var text = a.b.c how to modify the value of c according to text path to 4 , so that the result is { a: { b: { c: 4 } } } Mini Program s setData meth...
this is the singers list https: y.qq.com portal singe. ...
let arrn=[ { "MENU_URL": "serviceManage", "MENU_ID": "8619ca0b6fbc42649a27475e339d5d4a", "MENU_INFO": [ { "MENU_URL&quo...
A file about the size of 4MB. How to deal with it more efficiently and take up less resources. is primarily a read operation. ...
the parent can find the direct point of the subset, but the subset can find the parent. How to operate let arrn=[ { "MENU_URL": "dashboard", "MENU_ID": "104a580029c54e139210b7e87dca6d89...
for example, there is a whole row of JSON data and what I need is { productNo:xxx, key1:value1, key2:value2 } in this form ...
[{ "name": "", }, { "name": "", }, { "name": "&quo...
for example, the json string returned after the request { color:1, ... } the corresponding relationship between color numbers and Chinese: (1: blue 2: yellow 3: green) would like to ask, how to convert to Chinese to bind to the data object? (...
...
topic description js modify the contents of the json file and export and save sources of topics and their own ideas want to write a web page similar to online notes, the back end is not at all, just want to create a json file to record the number ...
for example, the string { "errcode ": 0, "errinfo ": "} can be formatted as when displayed. { "errcode":0, "errinfo":"" } Is there a plug-in implementation for ? of course not only this kind of value is a st...