如何使用多个筛选词和多个关键字从对象数组中进行筛选



我有一个对象数组和一个过滤器数组。数组中的每个对象都有一个id和一个"任务"数组。我想从每个对象内的任务数组中只使用"分配的任务"进行过滤。和";priority"钥匙。过滤器数组表示应该删除的内容。

对象数组:

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'
      },
    ],
  }
];

过滤器数组的例子:

filters: ["limegreen","red","Tom"];
使用这个过滤器数组,对象数组应该是这样的:
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'
      },
    ],
  }
];

过滤器的任何后续数组将只用于原始对象数组(任务),所以我需要保留原始列表以及。如果过滤器数组为空,则应该返回原始对象数组(TASKS)。

可以通过filterincludes实现

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 result = tasks.map(tasksGroup => {
tasksGroup.tasks = [...tasksGroup.tasks].filter(task => {
return !(filters.includes(task.priority) || filters.includes(task.assignedTo))
})
return tasksGroup
})
console.log(result)