从查询字符串中删除关联数组
Remove assoc array from query string
我想从查询 string
中删除一个数组变量 conditions
,它将是 window.location.search
。
- 第一个输出
Before
是初始字符串。
- 第二个是在移除
sub
之后
- 第三个仍然包含
conditions
。
我怎样才能摆脱它?
编辑
未编码的字符串是
conditions[0][0][field]=firstname&conditions[0][0][operator]=is&conditions[0][0][value]=John&conditions[1][0][field]=lastname&conditions[1][0][operator]=is&conditions[1][0][value]=Doe
这是申请所需要的。
var string = '?page=foo&sub=bar&conditions%5B0%5D%5B0%5D%5Bfield%5D=firstname&conditions%5B0%5D%5B0%5D%5Boperator%5D=is&conditions%5B0%5D%5B0%5D%5Bvalue%5D=John&conditions%5B1%5D%5B0%5D%5Bfield%5D=lastname&conditions%5B1%5D%5B0%5D%5Boperator%5D=is&conditions%5B1%5D%5B0%5D%5Bvalue%5D=Doe';
var params = new URLSearchParams(string);
console.log('Before', params.toString());
params.delete('sub');
console.log('remove sub works', params.toString());
params.delete('conditions');
console.log('conditions still exist', params.toString());
你想要这个吗?
var string =
"?page=foo&sub=bar&conditions%5B0%5D%5B0%5D%5Bfield%5D=firstname&conditions%5B0%5D%5B0%5D%5Boperator%5D=is&conditions%5B0%5D%5B0%5D%5Bvalue%5D=John&conditions%5B1%5D%5B0%5D%5Bfield%5D=lastname&conditions%5B1%5D%5B0%5D%5Boperator%5D=is&conditions%5B1%5D%5B0%5D%5Bvalue%5D=Doe";
const urlSearchParams = new URLSearchParams(string);
const params = Object.fromEntries(urlSearchParams.entries());
console.log({ params });
function getFilteredParams(params, filteredString) {
let obj = {};
for (const key in params) {
if (key.indexOf(filteredString) == -1) {
obj[key] = params[key];
}
}
return obj;
}
console.log(getFilteredParams(params, "conditions"));
我想从查询 string
中删除一个数组变量 conditions
,它将是 window.location.search
。
- 第一个输出
Before
是初始字符串。 - 第二个是在移除
sub
之后
- 第三个仍然包含
conditions
。
我怎样才能摆脱它?
编辑 未编码的字符串是
conditions[0][0][field]=firstname&conditions[0][0][operator]=is&conditions[0][0][value]=John&conditions[1][0][field]=lastname&conditions[1][0][operator]=is&conditions[1][0][value]=Doe
这是申请所需要的。
var string = '?page=foo&sub=bar&conditions%5B0%5D%5B0%5D%5Bfield%5D=firstname&conditions%5B0%5D%5B0%5D%5Boperator%5D=is&conditions%5B0%5D%5B0%5D%5Bvalue%5D=John&conditions%5B1%5D%5B0%5D%5Bfield%5D=lastname&conditions%5B1%5D%5B0%5D%5Boperator%5D=is&conditions%5B1%5D%5B0%5D%5Bvalue%5D=Doe';
var params = new URLSearchParams(string);
console.log('Before', params.toString());
params.delete('sub');
console.log('remove sub works', params.toString());
params.delete('conditions');
console.log('conditions still exist', params.toString());
你想要这个吗?
var string =
"?page=foo&sub=bar&conditions%5B0%5D%5B0%5D%5Bfield%5D=firstname&conditions%5B0%5D%5B0%5D%5Boperator%5D=is&conditions%5B0%5D%5B0%5D%5Bvalue%5D=John&conditions%5B1%5D%5B0%5D%5Bfield%5D=lastname&conditions%5B1%5D%5B0%5D%5Boperator%5D=is&conditions%5B1%5D%5B0%5D%5Bvalue%5D=Doe";
const urlSearchParams = new URLSearchParams(string);
const params = Object.fromEntries(urlSearchParams.entries());
console.log({ params });
function getFilteredParams(params, filteredString) {
let obj = {};
for (const key in params) {
if (key.indexOf(filteredString) == -1) {
obj[key] = params[key];
}
}
return obj;
}
console.log(getFilteredParams(params, "conditions"));