删除数组打字稿中的列表

时间:2019-10-24 15:28:47

标签: typescript

如何删除this.cols中的整行

this.cols数组的格式

this.cols = [
  { field: 'Id', header: 'ID', width: '70px' },
  { field: 'fullName', header: 'Full Name', width: '200px' }
  .....
];

//If does not exist in the access rights list, remove cols
for(var i=0; i<17; i++) {
  if (this.getAccessRights.indexOf('Id') !== -1) {

    //How do i remove the entire row in this.cols if it matches?

  }
}

this.cols数组的预期输出

this.cols = [
  { field: 'fullName', header: 'Full Name', width: '200px' }
  .....
];

试图使用remove()但遇到错误。这里的许多帖子都是关于python的,对我没有用。

2 个答案:

答案 0 :(得分:0)

this.cols = this.cols.filter(col => col.field !== 'Id');

Filter根据其中的条件逻辑返回一个新数组。因此,我们正在说的是,如果field不等于Id,则将该列推回数组。

答案 1 :(得分:0)

        const index = this.cols.indexOf(key);
        this.cols.splice(index, 1);

以上代码在indexOf()方法中给出了给定键的索引。然后使用splice()从给定索引中删除一个元素。

相关问题