Removing objects from an array based on another array

前端 未结 9 1561
南方客
南方客 2021-02-05 01:14

I have two arrays like this:

var arrayA = [\"Mike\", \"James\", \"Stacey\", \"Steve\"]
var arrayB = [\"Steve\", \"Gemma\", \"James\", \"Lucy\"]

9条回答
  •  無奈伤痛
    2021-02-05 01:24

    For smaller arrays I use:

    /* poormans sub for Arrays */
    
    extension Array where Element: Equatable {
    
        static func -=(lhs: inout Array, rhs: Array) {
    
            rhs.forEach {
                if let indexOfhit = lhs.firstIndex(of: $0) {
                    lhs.remove(at: indexOfhit)
                }
            }
        }
    
        static func -(lhs: Array, rhs: Array) -> Array {
    
            return lhs.filter { return !rhs.contains($0) }
        }
    }
    

提交回复
热议问题