如何删除 TypeScript 中的数组项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15292278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:01:02  来源:igfitidea点击:

How do I remove an array item in TypeScript?

arraystypescriptcollections

提问by Tim Almond

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?

我有一个在 TypeScript 中创建的数组,它有一个用作键的属性。如果我有那把钥匙,我怎样才能从中删除一个项目?

回答by blorkfish

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.splicefunction:

更好地使用该Array.prototype.splice功能:

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

回答by Malik Shahzad

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

回答by Idak

With ES6 you can use this code :

使用 ES6,您可以使用以下代码:

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

回答by Butsaty

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

回答by akash venugopal

You can use the splicemethod on an array to remove the elements.

您可以splice在数组上使用该方法来删除元素。

for example if you have an array with the name arruse 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 arrthen do this:

如果要删除命名数组的最后一个元素,arr请执行以下操作:

arr.splice(arr.length,1);

This will return arr with the last element deleted.

这将返回删除最后一个元素的 arr。

回答by Abdus Salam Azad

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

回答by Jamie Armour

Here's a simple one liner for removing an object by property from an array of objects.

这是一个简单的单行代码,用于从对象数组中按属性删除对象。

delete this.items[this.items.findIndex(item => item.item_id == item_id)];

or

或者

this.items = this.items.filter(item => item.item_id !== item.item_id);

回答by Joshua Michael Waggoner

Answer using TypeScript spread operator (...)

使用 TypeScript 展开运算符回答 (...)

// Your key
const key = 'two';

// Your array
const arr = [
    'one',
    'two',
    'three'
];

// Get either the index or -1
const index = arr.indexOf(key); // returns 0


// Despite a real index, or -1, use spread operator and Array.prototype.slice()    
const newArray = (index > -1) ? [
    ...arr.slice(0, index),
    ...arr.slice(index + 1)
] : arr;

回答by Sh. Pavel

One more solution using Typescript:

使用 Typescript 的另一种解决方案:

let updatedArray = [];
for (let el of this.oldArray) {
    if (el !== elementToRemove) {
        updated.push(el);
    }
}
this.oldArray = updated;

回答by Radu Linu

Use this, if you need to remove a given object from an array and you want to be sure of the following:

如果您需要从数组中删除给定的对象并且希望确保以下内容,请使用此选项:

  • the list is not reinitialized
  • the array length is properly updated
  • 该列表未重新初始化
  • 数组长度已正确更新
    const objWithIdToRemove;
    const objIndex = this.objectsArray.findIndex(obj => obj.id === objWithIdToRemove);
    if (objIndex > -1) {
      this.objectsArray.splice(objIndex, 1);
    }