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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM