TypeScript Array Remove

定義一個數組javascript

tags:any[];

方法1:filter
刪除並不留空位java

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

方法2:splice
刪除並不留空位git

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

方法3:remove
刪除並留空位,會有empty佔位github

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

示例代碼

示例代碼typescript

參考資料

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

相關文章
相關標籤/搜索