本文介绍了如何在TypeScript中删除数组项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在TypeScript中创建的数组,它具有一个用作键的属性.如果我有该密钥,如何从中删除一个项目?

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?

推荐答案

与JavaScript中的方法相同.

Same way as you would in JavaScript.

delete myArray[key];

请注意,这会将元素设置为undefined.

Note that this sets the element to undefined.

更好地使用 Array.prototype.splice 函数:

Better to use the Array.prototype.splice function:

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

这篇关于如何在TypeScript中删除数组项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:13