首页 > 解决方案 > 删除数组打字稿中的列表

问题描述

如何删除 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 的,这对我没有用。

标签: typescript

解决方案


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

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


推荐阅读