是否有通过查询字符串传递数组的标准方法?
明确起见,我有一个具有多个值的查询字符串,其中一个值将是数组值。我希望查询字符串值被视为数组-我不希望数组被分解,以便它与其他查询字符串变量难以区分。
另外,根据这篇文章的回答,作者建议不定义对数组的查询字符串支持。这准确吗?
编辑:
基于@Alex的回答,没有标准的方法来做到这一点,所以我的后续是什么是一个简单的方法来识别我正在阅读的参数是一个数组在PHP和Javascript?
将多个参数命名为相同的名称是否可以接受,这样我就知道它们属于一个数组?例子:
?myarray=value1&myarray=value2&myarray=value3...
或者这是一种不好的做法?
你可以使用这些函数,但要确保不要在对象键上使用“-”。
// convert obj to url params
function objToUrlParams(obj) {
let toUrlParams = (obj, prefex = '') => {
// create url params
let urlParams = "";
// loop through obj
for (let key in obj) {
let val = obj[key];
if (val == null) continue;
if (val == undefined) continue;
// if(val == '') continue;
// if val is an object then call toUrlParams
if (val instanceof Array) {
// convert val from Array to object
let valToObj = {};
val.forEach((v, i) => {
valToObj[i] = v;
});
val = valToObj;
}
let newPrefex = prefex + key;
if (val instanceof Object) {
urlParams += toUrlParams(val, newPrefex + '-');
} else {
urlParams += newPrefex + '=' + val;
}
urlParams += '&';
}
// remove last &
urlParams = urlParams.slice(0, -1);
// return url params
return urlParams;
}
// encodeURI
return encodeURI(toUrlParams(obj));
}
// convert url params to obj
function urlParamsToObj(urlParams) {
// decodeURI
urlParams = decodeURI(urlParams);
let toObj = (urlParams) => {
let obj = {};
let urlParamsArr = urlParams.split('&');
let subUrlParramsObj = {};
// loop through urlParams
for (let i = 0; i < urlParamsArr.length; i++) {
let item = urlParamsArr[i];
// get key and value
let key = item.split('=')[0];
let val = item.split('=')[1] ?? null;
let keys = key.split('-');
if (val == "null") {
val = null;
} else if (val == "undefined") {
val = undefined;
} else if (val == "true") {
val = true;
} else if (val == "false") {
val = false;
} else if (val == "NaN") {
val = NaN;
} else if (val == "Infinity") {
val = Infinity;
}
// if keys length is 1 then set obj[key] to val
if (keys.length == 1) {
// check if obj contains key
if (obj.hasOwnProperty(key)) {
// if obj[key] is an array then push val
if (obj[key] instanceof Array) {
obj[key].push(val);
} else {
// create array and push val
obj[key] = [obj[key], val];
}
} else {
obj[key] = val;
}
}
// if keys length is 2 then set obj[keys[0]][keys[1]] to val
else if (keys.length > 1) {
let key0 = keys[0];
// check if subUrlParramsObj contains keys[0]
if (!subUrlParramsObj[key0]) {
subUrlParramsObj[key0] = [];
}
// remove keys[0] from keys
keys.shift();
// join keys with -
key = keys.join('-');
let param = key + '=' + val;
// add param to subUrlParramsObj[keys[0]]
subUrlParramsObj[key0].push(param);
}
}
// loop through subUrlParramsObj
for (let key in subUrlParramsObj) {
// join subUrlParramsObj[key] with &
let val = subUrlParramsObj[key].join('&');
// set obj[key] to val
obj[key] = toObj(val);
}
// return obj
return obj;
}
return checkIfObjShouldBeArrayAndConvert(toObj(urlParams));
}
// check if object should be converted to array, if its keys are numbers
function checkIfObjShouldBeArrayAndConvert(obj) {
// if obj is an array
if (obj instanceof Array) {
// loop through obj
obj.forEach((item, i) => {
// check if item is an object
if (item instanceof Object) {
// convert item to array
obj[i] = checkIfObjShouldBeArrayAndConvert(item);
}
});
// return obj
return obj;
}
// check if all keys are numbers
let canConvertToArray = true;
for (let key in obj) {
// get value
let val = obj[key];
// check if value is an object or Array
if (val instanceof Object || val instanceof Array) {
obj[key] = checkIfObjShouldBeArrayAndConvert(val);
}
if (isNaN(key)) {
canConvertToArray = false;
}
}
// order obj by keys
let orderedObj = {};
Object.keys(obj).sort().forEach(function(key) {
orderedObj[key] = obj[key];
});
// check if the first key is 0
if (Object.keys(orderedObj)[0] != 0) {
canConvertToArray = false;
}
// check if the keys step is 1
let keys = Object.keys(orderedObj);
// loop through keys
for (let i = 0; i < keys.length - 1; i++) {
// get key
let key = keys[i];
// get next key
let nextKey = keys[i + 1];
// get key step
let keyStep = nextKey - key;
// check if key step is 1
if (keyStep != 1) {
canConvertToArray = false;
break;
}
}
// if all keys are numbers then convert obj to array
if (canConvertToArray) {
let arr = [];
for (let key in orderedObj) {
arr.push(orderedObj[key]);
}
return arr;
}
// return obj
return obj;
}
// add params to url
function addParamsToUrl(params, url = window.location.href) {
// check if url has params
if (url.indexOf('?') == -1) {
url += '?';
} else {
url += '&';
}
return url + params ?? '';
}
function addObjToUrl(obj, url = window.location.href) {
return addParamsToUrl(objToUrlParams(obj), url);
}
// extract params from url
function extractParamsFromUrl(url = window.location.href) {
return urlParamsToObj(url.split('?')[1]);
}
// test
let urlParams = objToUrlParams({
a: 1,
b: "zad",
c: {
d: 2,
e: "f"
},
j: [1, 2, 3, 4]
});
console.log(urlParams); // a=1&b=zad&c-d=2&c-e=f&j=1&j=2&j=3&j=4
let obj = urlParamsToObj(urlParams);
console.log(obj); // { "a": "1", "b": "zad", "j": [ "1", "2", "3", "4" ], "c": { "d": "2", "e": "f" } }
这对我来说很管用:
在link中,to属性具有值:
to="/filter/arr?fruits=apple&fruits=banana"
Route可以处理这个:
path="/filter/:arr"
对于多个数组:
to="filter/arr?fruits=apple&fruits=banana&vegetables=potato&vegetables=onion"
路线不变。
截图
更新:
为了实现这种字符串结构,query-string是最好的包。
例如:
import { stringify, parse } from 'query-string';
const queryParams={
fruits:['apple','banana'],
vegetables:['potato','onion']
}
//arrayFormat can be bracket or comma
stringify(queryParams, { arrayFormat: 'bracket' });