Filter array by indices

前端 未结 3 1263
闹比i
闹比i 2020-12-06 07:38

I have an array of elements. I also have an IndexSet that specifies which indices of the array need to be extracted into a new array. E.g.:

let array = [\"su         


        
3条回答
  •  有刺的猬
    2020-12-06 08:22

    You can use enumerated, filter and map like this

    let result = array
        .enumerated()
        .filter { indexSet.contains($0.offset) }
        .map { $0.element }
    

提交回复
热议问题