按索引过滤数组

按索引过滤数组,第1张

索引过滤数组

IndexSet
是递增整数的集合,因此可以 每个索引 映射 到相应的数组元素:

let array = ["sun", "moon", "star", "meteor"]let indexSet: IndexSet = [2, 3]let result = indexSet.map { array[
let result = indexSet.filteredIndexSet {  < array.count }.map { array[] }
] } // Magic happening here!print(result) // ["star", "meteor"]

假定所有索引对于给定数组均有效。如果不能保证,则可以过滤索引(如@dfri正确标记):



欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5620079.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存