Use swift Filter and Map

When dealing with huge data, for example, photo data: Basic information such as id, filepath, date, filename, etc. Flags required for apps such as isSelected, rating, openEye, etc.

There is something like this, but it is often necessary to extract various sequences as needed.

Use Filter and Map.

ImageViewModel


struct ImageViewModel {
    let id: String = ""
    let name: String = ""
    var isSelected: Bool = false
}

Get all the photos with isSelect set to true, When you want to put out the ids of those photos as a new array:

let imageViewModels = [ImageViewModel]()
let targetImageIds = imageViewModels.filter({ $0.isSelected }).map({ $0.id })

Recommended Posts

Use swift Filter and Map
[Swift UI] Use Map view
Install Xcode and use Hello Swift!
map, compactMap and flatMap
How to use map
[Swift] Constants and variables
[Swift] Let's use extension
How to use Map
Reasons to avoid for and map, filter, reduce, etc.
[Swift] Use for where rather than nesting for and if
[Java] How to use Map
[Java] How to use Map
[Swift] Let's use Segmented Control
Use Swift Package on Playground
[Swift] How to use UserDefaults
How to use Swift UIScrollView
Let's use Swift Firebase Firebase Auth
How to use Java Map
[Swift] Apply a gradation filter
[Swift] Templates and explanations of basic functions using Map Kit View
How to use StringBurrer and Arrays.toString.
[Java] Stream (filter, map, forEach, reduce)
Use java with MSYS and Cygwin
How to use EventBus3 and ThreadMode
[Swift] Another way to use Result?
Use Git with SourceTree and Eclipse
Use JDBC with Java and Scala.
Proper use of redirect_to and render
Use composite keys in Java Map.
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
Item 59: Know and use the libraries
[Swift] Advantages and disadvantages of Storyboard
Difference between Stream map and flatMap
Swift Network.framework Study 20200319 "Receive and Send"
Proper use of Mockito and PowerMock
[Swift] Protocol concept and definition method
[Swift] Property observers willSet and didSet
[Swift] How to connect TabBar with Storyboard Reference and also use NavigationController
Use MyBatis to get Map with key as identifier and value as Entity