如何基于多过滤词与多键对对象数组进行过滤
任务过滤需求与实现
需求说明
我有一个包含对象的数组,每个对象包含id和tasks数组。需要根据过滤数组移除每个对象的tasks数组中符合以下任一条件的项:
assignedTo的值存在于过滤数组中priority的值存在于过滤数组中
要求:
- 必须保留原数组
TASKS,所有过滤操作基于原数组 - 若过滤数组为空,直接返回原数组
原数据
const TASKS = [ { id: "To Do", tasks: [ { name: 'Get to work', created: 'July 7, 2022', assignedTo: 'Cody', description: 'I have to get to work', attachment: "", priority: 'red' }, { name: 'Pick up groceries', created: 'July 8, 2022', assignedTo: 'Bob', description: 'Go to grocery store', attachment: "", priority: 'limegreen' }, { name: 'Go home', created: 'July 9, 2022', assignedTo: 'Cody', description: 'Lets go back home', attachment: "" , priority: 'red' }, { name: 'Fall asleep', created: 'July 9, 2022', assignedTo: 'Tom', description: 'Time for bed', attachment: "" , priority: 'gold' }, ], }, { id: "Done", tasks: [ { name: 'Get up', created: 'July 7, 2022', assignedTo: 'Cody', description: 'Time to start the day', attachment: "1", priority: 'red' }, { name: 'Brush teeth', created: 'July 8, 2022', assignedTo: 'Bob', description: 'Brush', attachment: "", priority: 'limegreen' }, { name: 'Check e-mail', created: 'July 11, 2022', assignedTo: 'Jake', description: 'Anyhting new?', attachment: "" , priority: 'red' }, { name: 'Walk dog', created: 'July 12, 2022', assignedTo: 'Bob', description: 'He needs to go out', attachment: "" , priority: 'gold' }, ], }, { id: "In Progress", tasks: [ { name: 'Wash Clothes', created: 'July 8, 2022', assignedTo: 'Bob', description: 'Brush', attachment: "", priority: 'limegreen' }, { name: 'Go to doctors', created: 'July 10, 2022', assignedTo: 'Jake', description: 'Anyhting new?', attachment: "" , priority: 'red' }, { name: 'Get gasoline', created: 'July 9, 2022', assignedTo: 'Jonathan', description: 'He needs to go out', attachment: "" , priority: 'gold' }, ], } ];
过滤数组示例
const filters = ["limegreen","red","Tom"];
期望过滤结果
const filteredTasks = [ { id: "To Do", tasks: [ ], }, { id: "Done", tasks: [ { name: 'Walk dog', created: 'July 12, 2022', assignedTo: 'Bob', description: 'He needs to go out', attachment: "" , priority: 'gold' }, ], }, { id: "In Progress", tasks: [ { name: 'Get gasoline', created: 'July 9, 2022', assignedTo: 'Jonathan', description: 'He needs to go out', attachment: "" , priority: 'gold' }, ], } ];
实现代码
function filterTasks(tasksData, filters) { // 过滤数组为空时返回原数组的深拷贝,避免修改原数据 if (!filters || filters.length === 0) { return JSON.parse(JSON.stringify(tasksData)); } // 遍历每个任务分组,过滤符合条件的任务 return tasksData.map(group => ({ ...group, tasks: group.tasks.filter(task => { // 保留既不在过滤名单中的assignedTo,也不在过滤名单中的priority的任务 return !filters.includes(task.assignedTo) && !filters.includes(task.priority); }) })); } // 使用示例 const filters = ["limegreen","red","Tom"]; const filteredTasks = filterTasks(TASKS, filters); console.log(filteredTasks);
代码说明
- 当过滤数组为空时,通过
JSON.parse(JSON.stringify())深拷贝原数组,确保原数组不被修改 - 使用
map遍历每个任务分组,复制分组的id等属性,仅对tasks数组进行过滤 filter方法筛选出assignedTo和priority都不在过滤数组中的任务,保留这些项
内容的提问来源于stack exchange,提问作者Cody Henry
相关产品推荐
相关产品推荐

