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

前端 未结 7 1796
走了就别回头了
走了就别回头了 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:10

    In Swift 3 I simply do:

    arr = arr.filter { $0 != "a" } 
    

    .filter, .sort and .map are great for saving time and solve lots of problems with little code.

    This article has good examples and explain the differences and how they work: https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/

提交回复
热议问题