我想知道是否有任何有效的方法来过滤自定义对象的数组,例如我下面的方法。我有一个Author类和一个Book类,可以有多个具有不同属性的作者,例如书状态的枚举,作者写的书注释以及书状态发生变化的数组。我想按不同的状态,作者和日期进行过滤,其中所有4个参数都是可选的。
class Author : Equatable {
let name : String
let id = UUID()
}
class Book : Equatable {
let date = Date()
var comments : [Comment] = []
var writtenBy : [Author] = []
var state : State = .available {
didSet {
updateState(timestamp: Date())
}
}
}
enum State : String, CaseIterable {
case available
case unavailable
case missing
}
class CollectionOfBooks {
private static var arrayOfBooks : [Book] = []
static func getAllBooks(state: State?, authorId: UUID?, fromDate: Date?, toDate : Date?) -> [Book] {
let filteredArray = arrayOfBooks.filter({$0.state == state})
filteredArray = arrayOfBooks.filter....
filteredArray = arrayOfBooks.filter....
filteredArray = arrayOfBooks.filter....
return filtered array
// what is the most efficient way of sorting the my array?
//At the moment i only know how to achieve this by filtering one parameter at a time.
}
}
答案 0 :(得分:1)
您可以在一个条件中合并所有条件。
<div *ngIf="gotDataMapping">Content to render when condition is true.</div>