How do I remove an array item in TypeScript?

后端 未结 14 2281
渐次进展
渐次进展 2020-12-07 07:42

I have an array that I\'ve created in TypeScript and it has a property that I use as a key. If I have that key, how can I remove an item from it?

相关标签:
14条回答
  • 2020-12-07 08:18

    let departments is an array. You want to remove an item from this array.

    departments: string[] = [];
    
     removeDepartment(name: string): void {
        this.departments = this.departments.filter(item => item != name);
      }
    
    0 讨论(0)
  • 2020-12-07 08:20

    If array is type of objects, then the simplest way is

    let foo_object // Item to remove
    this.foo_objects = this.foo_objects.filter(obj => obj !== foo_object);
    
    0 讨论(0)
提交回复
热议问题