如何在TypeScript中删除数组项?

编程入门 行业动态 更新时间:2024-10-27 08:38:40
本文介绍了如何在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中删除数组项?

本文发布于:2023-10-18 02:17:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何在   TypeScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!