使用切片从数组中删除元素 [英] Remove element from array, using slice

查看:89
本文介绍了使用切片从数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用切片从我的数组中删除一个元素,但我无法让它工作,看看这段代码。

I am trying to remove a element from my array using slice, but i can't get it to work, look at this piece of code.

    console.log(this.activeEffects); // Prints my array
    console.log(this.activeEffects.slice(0,1)); // Remove from index 0, and remove one.
    console.log(this.activeEffects); // Prints array again, this time my element should be gone

结果是。

所以从这里得到的是,首先阵列是完整的,应该是这样。然后它打印出切片的数组。最后第三个应该是空的?或者?

So what is get from this is, at first the array is whole, as it should be. Then its prints what is sliced of the array. Finally the third should be empty? or?

推荐答案

我相信你正在寻找 splice 。来自 W3学校:

I believe you're looking for splice. From W3 Schools:


splice()方法在数组中添加/删除项目,并返回删除的项目。

The splice() method adds/removes items to/from an array, and returns the removed item(s).

Take看一下该页面上的例子;用例类似于你想要达到的目的。

Take a look at the example on that page; the use case there is similar to what you want to achieve.

编辑: Nicosunshine建议的MDN的替代链接;关于命令的更多信息。

Alternative link to MDN, as suggested by Nicosunshine; much more information about the command there.

这篇关于使用切片从数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆