Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从对象列表中删除项目-Typescript_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 从对象列表中删除项目-Typescript

Javascript 从对象列表中删除项目-Typescript,javascript,angular,typescript,Javascript,Angular,Typescript,我想从列表中删除特定对象。对象模型如下所示: export class Task { taskId: number; projectId: Project; userId: User; state: string; description: string; } 我创建了任务对象列表,并希望删除具有特定taskId的任务 moveTaskInProgress(task: Task) { console.log('The task is in progress...'

我想从列表中删除特定对象。对象模型如下所示:

export class Task {
  taskId: number;
  projectId: Project;
  userId: User;
  state: string;
  description: string;
}
我创建了任务对象列表,并希望删除具有特定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);
  }
我有一个接收任务的方法,调用一个更新某些属性的方法,然后我想从列表中删除该任务。

但似乎什么也没发生。你能帮我吗

存在拼写错误,请使用tasks.taskid代替task


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


您可以只发送ID,而不发送完整的对象

removeItemFromAListOfTasks(id: any, listOfTasks: Task[]) {
  return listOfTasks.filter(tast => task.taskId !== id);
}
现在可以称之为

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

过滤数组将返回一个包含原始项子集的新数组。它不会修改原始数组

相反,你需要拼接它

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);
}
这是假设任务来自数组。否则,您需要查找索引:

const index = listOfTasks.findIndex(x => x.taskId === task.taskId);

您的示例中的任务变量是什么?这里,
filteredList
将包含所有taskId与
task.taskId
不同的任务。你有什么问题?筛选器不会修改原始数组-它返回一个新数组。如果你想从原始数组中删除一个项目,你需要拼接它。@KurtHamilton我更新了我的帖子。嗯,任务似乎仍然在列表中。“之后,我想从列表中删除任务。”哪个列表?为什么这比OP的原始代码更有效?我们可以争辩说,对于像
this.openTasks=this.openTasks.filter()
这样的内置过滤器,可以跳过
removietemfromalistoftasks()函数。无论如何,他想要一个函数。因此,我试图通过只发送ID来减少开销。如果没有匹配的元素,将返回-1。因此,在执行
splice()
操作之前,应该检查它。@MichaelD我声明了任务起源于数组的假设,但您提出了一个很好的观点。我已经修改了我的答案。谢谢