How to remove common items from two struct arrays in Swift

前端 未结 3 1499
旧时难觅i
旧时难觅i 2021-01-16 15:39

In my app I have two struct arrays and I want to remove common items from one of them. My struct:

struct PeopleSelectItem {
    var name = \"\"
    var id =          


        
3条回答
  •  天命终不由人
    2021-01-16 16:20

    • Get an array of all ids in selectedPeople

      let selectedPeopleIDs = selectedPeople.map { $0.id }
      
    • Filter the items whose id is not in the array

      let filteredPeople = people.filter { !selectedPeopleIDs.contains($0.id) }
      

提交回复
热议问题