我有一个看起来像这样的查询字符串...
let badString = "dogs=c,b,a&cats=z,y,x";
使用vanilla javascript或lodash框架我试图将键和值重新排列成字母顺序,所以它最终会像......
let goodString = "cats=x,y,z&dogs=a,b,c"
答案 0 :(得分:3)
const goodString = badString.split('&').sort().map(val => {
let [k,v] = val.split('=');
return [k, v.split(',').sort().join(',')].join('=');
}).join('&');
答案 1 :(得分:1)
let badString = "dogs=c,b,a&cats=z,y,x";
var array = badString.split("&");
var newArray = [];
array.forEach(function(e) {
newArray.push(e.split('=')[0] + '=' + (e.split('=')[1]).split(',').sort().join());
});
var goodString = newArray.sort().join('&');
console.log(goodString);