Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter from an array of objects using multiple filter words and multiple keys

I have an array of objects and an array of filters. Each object in the array has an id and an array of "tasks." I would like to filter from the array of tasks within each object using only the "assignedTo" and "priority" keys. The array of filters represents what should be removed.

Array of objects:

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

Example of an array of filters:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

filters: ["limegreen","red","Tom"];

Using this array of filters the object array should look like:

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

Any subsequent array of filters will only be used on the original object array (TASKS) so I need to preserve the original list as well. If the array of filters is empty, the original object array (TASKS) should be returned.

>Solution :

You can achieve that with filter and includes

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading