本文翻译自: How do I remove an array item in TypeScript?

I have an array that I've created in TypeScript and it has a property that I use as a key. 我有一个在TypeScript中创建的数组,它具有一个用作键的属性。 If I have that key, how can I remove an item from it? 如果我有该钥匙,如何从中删除一个物品?

参考: https://stackoom.com/question/12ADe/如何在TypeScript中删除数组项

Same way as you would in JavaScript. 与JavaScript中的方式相同。

delete myArray[key];

Note that this sets the element to undefined . 请注意,这会将元素设置为undefined

Better to use the Array.prototype.splice function: 最好使用Array.prototype.splice函数:

const index = myArray.indexOf(key, 0);
if (index > -1) {
   myArray.splice(index, 1);

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);

It is my solution for that: 这是我的解决方案:

onDelete(id: number) {
    this.service.delete(id).then(() => {
        let index = this.documents.findIndex(d => d.id === id); //find index in your array
        this.documents.splice(index, 1);//remove element from array
    event.stopPropagation();

With ES6 you can use this code : 使用ES6,您可以使用以下代码:

removeDocument(doc){
   this.documents.forEach( (item, index) => {
     if(item === doc) this.documents.splice(index,1);

You can use the splice method on an array to remove the elements. 您可以在数组上使用splice方法删除元素。

for example if you have an array with the name arr use the following: 例如,如果您有一个名为arr的数组,请使用以下命令:

arr.splice(2,1);

so here the element with index 2 will be the starting point and the argument 2 will determine how many elements to be deleted. 因此这里索引为2的元素将作为起点,而参数2将确定要删除的元素数。

If you want to delete the last element of the array named arr then do this: 如果要删除名为arr的数组的最后一个元素,请执行以下操作:

arr.splice(arr.length,1);

This will return arr with the last element deleted. 这将返回arr并删除最后一个元素。

I have an array that I've created in TypeScript and it has a property that I use as a key. 我有一个在Typ //判读数据不能为空且不能为undefined if(row != "" && row != undefined){ this.newArr.push(row);//将不为空的数据push到新数组
引言:业务需要在typescript数组的指定位置插入元素,typescript自带的push方法只能向数组末尾添加元素,不满足业务的需要,typescript的splice方法可以完成此功能。 1、描述: splice()方法更改数组的内容,在删除旧元素的同时添加新元素。 2、语法: array.splice(index, howMany, [element1][, ..., elementN]); index:开始更改数组的索引。 howMany:一个整数,指示要删除的旧数组元素的数量,如果ho.
对象字面量,也可以说是按照对象结构直接进行描述的一种方式,其适用场景为:明确知道对象拥有的属性及对应的类型,(PS:接口其实和属性类型映射的方式其实可以看成是一种字面量类型)如: let obj: { a: string } = { a: 'demo' 空对象字面量 ({}),可以指代除了 undefined 和 null 之外的任何类型,但不推荐使用这种方式 显式注解为 object 类型,可以指代 JS 的对象类
TypeScript ,装饰器是一种特殊的声明,它可以被附加到类声明、方法、属性或参数上,以修改类的行为。装饰器本身是一个函数,它接收三个参数: 1. 对于类声明来说,是类的构造函数;对于方法、属性和参数来说,是它们所在的类的原型对象。 2. 对于方法和属性来说,是成员的名字;对于参数来说,是参数的索引。 3. 装饰器的返回值会被忽略,因此可以是任何值,甚至是 undefined。 下面是一个使用装饰器修饰类的例子: ```typescript function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) { return class extends constructor { newProperty = "new property"; hello = "override"; @classDecorator class Greeter { property = "property"; hello: string; constructor(m: string) { this.hello = m; console.log(new Greeter("world").hello); 在上面的例子,`classDecorator` 是一个装饰器函数,它将传入的类进行了修饰,添加了一个新的属性和一个新的值。`@classDecorator` 是将装饰器应用到类声明上的语法。 要启用对装饰器的实验支持,需要在 TypeScript 的编译选加入 `--experimentalDecorators` 参数。可以在命令行使用 `tsc --experimentalDecorators file.ts` 编译单个文件,或者在 `tsconfig.json` 文件设置 `"experimentalDecorators": true` 来启用所有文件的实验支持。