gpt4 book ai didi

javascript - 有没有更优雅的方法从数组中删除元素?

转载 作者:行者123 更新时间:2023-11-28 14:41:42 25 4
gpt4 key购买 nike

我现在从数组中删除元素的方式是这样的

var indexToRemove = newSections.indexOf(newSections.find((section) => section.id === parseInt(sectionId)));
newSections.splice(indexToRemove, 1);

但是,我希望能够删除我的元素。

array.remove(element)

我怎样才能完成这样的事情?

最佳答案

没有 API 可以在适当的位置执行此类操作,但您可以使用 Array.filter 执行类似的操作。

let words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"];

words = words.filter(word => word != "spray");

在上面的示例中,words 将不包含单词 spray

关于javascript - 有没有更优雅的方法从数组中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47851862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com