如何从快速数组中删除多个项目? [英] How to remove multiple items from a swift array?
本文介绍了如何从快速数组中删除多个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有一个数组
var array = [1, 2, 3, 4]
我要删除索引1处的项目,然后删除索引3处的让它进入for循环".
I want to remove item at index 1 then at index 3 "let it be in a for loop".
但是删除索引1处的项目会将索引3处的项目移动到索引2,从而使第二次删除变得混乱.
But removing the item at index 1 will move the item at index 3 to index 2, thus messing up the second removal.
有什么建议吗?
推荐答案
给出数组
var numbers = [1, 2, 3, 4]
和要删除的索引Set
let indexesToRemove: Set = [1, 3]
您要删除值"2"和"4".
You want to remove the values "2" and "4".
只要写
numbers = numbers
.enumerated()
.filter { !indexesToRemove.contains($0.offset) }
.map { $0.element }
结果
print(numbers) // [1, 3]
这篇关于如何从快速数组中删除多个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文