如何过滤具有唯一属性的对象数组?
How to filter an array of objects with unique properties?
请原谅下面这个人为的例子,但我怎样才能像这样过滤?使用 Set
去重不是一种选择,因为我的真实数据对象有另一个 属性,每个对象都是唯一的。
struct MyDataObject {
var startDate: Date
var endDate: Date
}
let dataObject1 = MyDataObject(startDate: Date().startOfDay(), endDate: Date().startOfDay())
let duplicateDataObject = MyDataObject(startDate: Date().startOfDay(), endDate: Date().startOfDay())
let array = [dataObject1, duplicateDataObject]
//How to filter to end up with an array of data objects with a unique start date?
https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md
import Algorithms
array.uniqued(on: \.startDate)
或者,如果您需要更多地控制选择哪些元素:
array.uniqued(on: \.startDate) { [[=11=], ].max(by: \.endDate)! }
import struct OrderedCollections.OrderedDictionary
public extension Sequence {
@inlinable func uniqued<Subject: Hashable>(
on projection: (Element) throws -> Subject,
uniquingWith combine: (Element, Element) throws -> Element
) rethrows -> [Element] {
try OrderedDictionary(keyed(by: projection), uniquingKeysWith: combine)
.values
.elements
}
@inlinable func max<Comparable: Swift.Comparable>(
by getComparable: (Element) throws -> Comparable
) rethrows -> Element? {
try self.max {
try getComparable([=12=]) < getComparable()
}
}
}
public extension Sequence {
@inlinable func keyed<Key: Hashable>(
by key: (Element) throws -> Key
) rethrows -> [KeyValuePairs<Key, Element>.Element] {
try map { (try key([=13=]), [=13=]) }
}
}
您的意思是过滤以便只保留在原始集合中恰好出现一次的元素吗?
import OrderedCollections
struct A {
let id: String
let thing: String
}
let things: [A] = [
.init(id: "1", thing: "thing"),
.init(id: "2", thing: "thing"),
.init(id: "3", thing: "thing"),
.init(id: "2", thing: "thing"),
.init(id: "1", thing: "thing"),
.init(id: "4", thing: "thing"),
.init(id: "2", thing: "thing"),
]
let uniqueThings = OrderedDictionary<String, [A]>(grouping: things, by: \.id)
.filter { [=10=].value.count == 1 }
.values
仅给出具有唯一 ID(在此示例中为 3 和 4)的 A
或者你的意思只是去重,比如 https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md uniqued(on:)
如果是的话,如果有多个元素,应该保留哪个元素,第一个看到的,最后一个等等?
请原谅下面这个人为的例子,但我怎样才能像这样过滤?使用 Set
去重不是一种选择,因为我的真实数据对象有另一个 属性,每个对象都是唯一的。
struct MyDataObject {
var startDate: Date
var endDate: Date
}
let dataObject1 = MyDataObject(startDate: Date().startOfDay(), endDate: Date().startOfDay())
let duplicateDataObject = MyDataObject(startDate: Date().startOfDay(), endDate: Date().startOfDay())
let array = [dataObject1, duplicateDataObject]
//How to filter to end up with an array of data objects with a unique start date?
https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md
import Algorithms
array.uniqued(on: \.startDate)
或者,如果您需要更多地控制选择哪些元素:
array.uniqued(on: \.startDate) { [[=11=], ].max(by: \.endDate)! }
import struct OrderedCollections.OrderedDictionary
public extension Sequence {
@inlinable func uniqued<Subject: Hashable>(
on projection: (Element) throws -> Subject,
uniquingWith combine: (Element, Element) throws -> Element
) rethrows -> [Element] {
try OrderedDictionary(keyed(by: projection), uniquingKeysWith: combine)
.values
.elements
}
@inlinable func max<Comparable: Swift.Comparable>(
by getComparable: (Element) throws -> Comparable
) rethrows -> Element? {
try self.max {
try getComparable([=12=]) < getComparable()
}
}
}
public extension Sequence {
@inlinable func keyed<Key: Hashable>(
by key: (Element) throws -> Key
) rethrows -> [KeyValuePairs<Key, Element>.Element] {
try map { (try key([=13=]), [=13=]) }
}
}
您的意思是过滤以便只保留在原始集合中恰好出现一次的元素吗?
import OrderedCollections
struct A {
let id: String
let thing: String
}
let things: [A] = [
.init(id: "1", thing: "thing"),
.init(id: "2", thing: "thing"),
.init(id: "3", thing: "thing"),
.init(id: "2", thing: "thing"),
.init(id: "1", thing: "thing"),
.init(id: "4", thing: "thing"),
.init(id: "2", thing: "thing"),
]
let uniqueThings = OrderedDictionary<String, [A]>(grouping: things, by: \.id)
.filter { [=10=].value.count == 1 }
.values
仅给出具有唯一 ID(在此示例中为 3 和 4)的 A
或者你的意思只是去重,比如 https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md uniqued(on:)
如果是的话,如果有多个元素,应该保留哪个元素,第一个看到的,最后一个等等?