URL过滤器删除功能代码失效,寻求技术解决方案
问题:删除URL中指定的过滤器条目
我需要实现一个函数,用来删除URL中传递的指定过滤器。举个例子:
原URL:&data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5;
当删除Employee过滤器时,输出应该是:&data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Time|Current+Month+Flag|1;&decimals=5;
原因是Employee是include|value|Employee这个主题区域下唯一的过滤器,所以要删除整个对应的filterList条目。
下面是我自己写的函数代码,但它无法实现预期需求:
function deleteUrlSection(url, key, deleteValue) { const sections = url.split(';'); let updatedSections = []; for (let i = 0; i < sections.length; i++) { let section = sections[i]; let keyIndex = section.indexOf(key); if (keyIndex !== -1) { let values = section.substring(keyIndex).split('|'); let updatedValues = []; for (let j = 0; j < values.length; j++) { if (values[j] !== deleteValue) { updatedValues.push(values[j]); } } if (updatedValues.length > 0) { updatedSections.push(updatedValues.join('|')); } } else { updatedSections.push(section); } } return updatedSections.join(';'); } //test 1 let url = "&data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5;"; let key = "Region"; let deleteV = "Asia"; console.log("New Url ", "", deleteUrlSection(url, key, deleteV)); //expected output //&data=true&filterList=include|value|Location|Region|Europe;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5; //test 2 let url2 = "&data=true&filterList=include|value|Location|Region|Europe;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5;"; let key2 = "Region"; let deleteV2 = "Europe"; console.log("New Url2 ", "", deleteUrlSection(url2, key2, deleteV2)); //expected output //&data=true&filterList=include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5; //test 3 let url3 = "&data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5;"; let key3 = "Current+Month+Flag"; let deleteV3 = "1"; console.log("New Url3 ", "", deleteUrlSection(url, key3, deleteV3)); //expected output //&data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Employee|Employee+Type|Employee;&decimals=5;
过滤器规则说明
- 过滤器格式:
include|value|subjectArea|key|filter1|filter2...,每个filterList条目可包含多个过滤器 - 存在过滤器与subjectArea同名的情况,但仅需删除过滤器部分
- 如果删除目标过滤器后,该条目下没有剩余过滤器,需要删除整个filterList条目
我的思路是通过key(位于第3个竖线|之后)定位filterList条目,删除目标过滤器;如果删除后只剩一个过滤器就删除整个条目,但当前代码无法实现这个逻辑,需要修正。
修正后的函数实现
function deleteUrlSection(url, key, deleteValue) { const sections = url.split(';'); const updatedSections = []; for (const section of sections) { // 跳过空段 if (!section) continue; // 只处理包含目标key的段 if (!section.includes(key)) { updatedSections.push(section); continue; } // 拆分出前缀(比如"filterList=")和过滤器内容 const [prefix, ...filterParts] = section.split('='); const fullFilter = filterParts.join('='); const filterSegments = fullFilter.split('|'); // 确认key在规则指定的位置(第3个竖线后,索引为3) if (filterSegments.indexOf(key) !== 3) { updatedSections.push(section); continue; } // 提取key之后的所有过滤器值 const filters = filterSegments.slice(4); // 过滤掉要删除的值 const remainingFilters = filters.filter(filter => filter !== deleteValue); if (remainingFilters.length === 0) { // 没有剩余过滤器,直接跳过整个条目 continue; } else { // 重新构建完整的filter条目 const newFilter = [ ...filterSegments.slice(0, 4), ...remainingFilters ].join('|'); updatedSections.push(`${prefix}=${newFilter}`); } } return updatedSections.join(';'); } // 测试用例执行 // test 1 let url = "&data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5;"; let key = "Region"; let deleteV = "Asia"; console.log("New Url ", deleteUrlSection(url, key, deleteV)); // 预期输出: &data=true&filterList=include|value|Location|Region|Europe;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5; // test 2 let url2 = "&data=true&filterList=include|value|Location|Region|Europe;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5;"; let key2 = "Region"; let deleteV2 = "Europe"; console.log("New Url2 ", deleteUrlSection(url2, key2, deleteV2)); // 预期输出: &data=true&filterList=include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5; // test 3 let url3 = "&data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Time|Current+Month+Flag|1;include|value|Employee|Employee+Type|Employee;&decimals=5;"; let key3 = "Current+Month+Flag"; let deleteV3 = "1"; console.log("New Url3 ", deleteUrlSection(url3, key3, deleteV3)); // 预期输出: &data=true&filterList=include|value|Location|Region|Europe|Asia;include|value|Employee|Employee+Type|Employee;&decimals=5;
关键逻辑说明
- 精准定位目标条目:通过检查key是否在过滤器的第4个位置(索引3),避免误匹配与subjectArea同名的过滤器
- 独立处理过滤器部分:提取key之后的所有过滤器值,单独过滤要删除的内容
- 判断条目保留与否:如果过滤后没有剩余过滤器,直接丢弃整个条目;否则重新构建完整的filter段
- 保留其他URL参数:不包含目标key的段直接保留,保证URL其他部分不受影响
内容的提问来源于stack exchange,提问作者Jakub97
相关产品推荐
相关产品推荐

