How do I remove an array item in TypeScript?

后端 未结 14 2284
渐次进展
渐次进展 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:07

    Just wanted to add extension method for an array.

    interface Array {
          remove(element: T): Array;
        }
    
        Array.prototype.remove = function (element) {
          const index = this.indexOf(element, 0);
          if (index > -1) {
            return this.splice(index, 1);
          }
          return this;
        };
    

提交回复
热议问题