TypeScript Array Remove

定义一个数组

tags:any[];

方法1:filter
删除并不留空位

this.tags = this.tags.filter(tag => tag !== removedTag);

方法2:splice
删除并不留空位

const index = this.tags.indexOf(removedTag, 0);
if (index > -1) {
    this.tags.splice(index, 1);
}

方法3:remove
删除并留空位,会有empty占位

const index = this.tags.indexOf(removedTag, 0);
if (index > -1) {
    delete this.tags[index];
}

示例代码

示例代码

参考资料

How do I remove an array item in TypeScript?
Deleting array elements in JavaScript - delete vs splice

posted @ 2019-04-05 17:33  Lulus  阅读(3737)  评论(0编辑  收藏  举报