2

I want to remove a specific object from a list. The object model looks like this:

export class Task {
  taskId: number;
  projectId: Project;
  userId: User;
  state: string;
  description: string;
}

I created a list of Task objects and I want to delete a Task that has a specific taskId.

 moveTaskInProgress(task: Task) {
    console.log('The task is in progress...');
    task.state = 'IN_PROGRESS';
    this.taskService.updateTask(task).subscribe((nbOfUpdates) => {
      console.log('Number of updates: ' + JSON.stringify(nbOfUpdates));
      this.removeItemFromAListOfTasks(task, this.openTasks);
      console.log('List of task task: ' + JSON.stringify(this.openTasks));
    });
 }

  removeItemFromAListOfTasks(task: Task, listOfTasks: Task[]) {
    let filteredList: Task[];
    filteredList = listOfTasks.filter(item => item.taskId !== task.taskId);
  }

I have a method that receive a task, call a method that updates some properties, and after that i want to delete the task from the list.
But it seems that nothing happen. Could you, please, help me with this?

5
  • 1
    What is the task variable in your example? Commented Feb 20, 2020 at 12:38
  • 1
    Here, filteredList will contain all tasks that have a taskId different from whatever task.taskId is. What's your problem? Commented Feb 20, 2020 at 12:41
  • 1
    Filter will not modify the original array - it returns a new array. If you want to delete an item from the original array, you need to splice it. Commented Feb 20, 2020 at 12:43
  • @KurtHamilton I updated my post. Well it seems that the task it's still in the list. Commented Feb 20, 2020 at 12:43
  • 2
    "after that i want to delete the task from the list." which list? Commented Feb 20, 2020 at 12:45

3 Answers 3

4

Filtering an array returns a new array with a subset of the original items. It does not modify the original array.

Instead you need to splice it.

moveTaskInProgress(task: Task) {
  console.log('The task is in progress...');
  task.state = 'IN_PROGRESS';
  this.taskService.updateTask(task).subscribe((nbOfUpdates) => {
    console.log('Number of updates: ' + JSON.stringify(nbOfUpdates));
    this.removeItemFromAListOfTasks(task, this.openTasks);
    console.log('List of task task: ' + JSON.stringify(this.openTasks));
  });
}

removeItemFromAListOfTasks(task: Task, listOfTasks: Task[]) { 
  const index = listOfTasks.indexOf(task);
  if (index === -1) {
    // the task doesn't exist in the array, no need to continue
    return;
  }

  // delete 1 item starting at the given index
  listOfTasks.splice(index, 1);
}

This is assuming that the task originated from the array. Otherwise you will need to find the index:

const index = listOfTasks.findIndex(x => x.taskId === task.taskId);
Sign up to request clarification or add additional context in comments.

2 Comments

If there are no matching elements, indexOf will return -1. So it should be checked before the splice() operation.
@MichaelD I stated the assumption that the task originated in the array, but you make a good point. I've amended my answer. Thanks
0

You could send only the ID instead of sending the complete object.

removeItemFromAListOfTasks(id: any, listOfTasks: Task[]) {
  return listOfTasks.filter(tast => task.taskId !== id);
}

Now it can be called like

this.openTasks = this.removeItemFromAListOfTasks(task.taskId, this.openTasks);

2 Comments

and why would this work better than OP's original code?
We could argue the removeItemFromAListOfTasks() function could be skipped for an in-place filter like this.openTasks = this.openTasks.filter(). He wanted a function anyway. So I tried to reduce the overhead by sending in only the ID.
-1

there is spelling mistake use tasks.tasskId instead of task.


tasks: Task[]; // here I received some objects from a HTTP request. 
filteredList: Task[];
filteredList = listOfTasks.filter(item => item.taskId !== tasks.taskId);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.