角度从列表中删除列

时间:2019-05-23 14:20:04

标签: angular types

我正在尝试从列表中删除一列,在删除之前,我将列表分配给另一个列表进行备份。但是,从列表中删除列名后,备份列表中的该列也将被删除。

 this.commonService
  .fetchContractByDate(this.authService.loggedUser.companyId, this.startDate, this.endDate)
  .subscribe((response: any) => {
    const clientName = this.clients;

    this.contractListbyClient = response.data;
    this.allList = response.data;
    this.forexport(clientName, contractList);

    for (const contract of this.contractListbyClient) {
      for (const selected of this.selection.selected) {
        if (selected.name === 'startDate' || selected.name === 'endDate') {
          contract[selected.display] = this.commonService.getFormattedDate(contract[selected.name]);
        } else {
          contract[selected.display] = contract[selected.name];
        }
      }

      for (const item of this.columns) {
        delete contract[item.name];
      }
    }

在上面的代码中,从 this.contractListbyClient 中选择的列被删除了,但是相同的更改也反映在 this.allList 中。谁能告诉我如何保持列表副本不变

2 个答案:

答案 0 :(得分:0)

这是因为对于对象,变量的值是引用。因此,当您传递一个对象并更改其成员时,这些更改会在函数外部持续存在。这使其看起来像通过引用传递。

要解决此问题,我们应该正确地克隆该对象,一种简单的操作方法如下:

docker rm

答案 1 :(得分:0)

尝试一下:

this.contractListbyClient = Object.assign({}, response.data); this.allList = Object.assign({}, response.data);