How to remove an element of a given value from an array in Swift

前端 未结 7 1800
走了就别回头了
走了就别回头了 2020-12-28 11:34

I want to remove all elements of value x from an array that contains x, y and z elements

let arr = [\'a\', \'b\', \'c\', \'b\']

How can I r

7条回答
  •  粉色の甜心
    2020-12-28 12:17

    If you have more than one element to remove, thanks to first answer.

     var mainArray = ["a", "b", "qw", "qe"]
     let thingsToRemoveArray = ["qw", "b"] 
    
    
            for k in thingsToRemoveArray {
                mainArray =  mainArray.filter {$0 != k}
              }
    

提交回复
热议问题