从日期数组中获取日期数组
Get array of dates from an array of dates
我有一个 Date
对象数组,如下例所示:
let dates = [date1, date2, date3, date4, date5]
如何为具有相同 day of month
、month
和 year
.
的日期创建子数组
示例:
date1 = 20/04/2020
date2 = 19/04/2020
date3 = 19/04/2020
date4 = 18/04/2020
date5 = 18/04/2020
...
有了这个,我想要以下内容:
let group1 = [date1]
let group2 = [date2, date3]
let group3 = [date4, date5]
let group4 = //...
我尝试了 DateComponents
和 .map
、.filter
等功能,但不知何故我无法做到这一点。
我能得到提示吗?
谢谢。
只需创建跟踪年月日的结构。
struct DateCluster {
let day: Int
let month: Int
let year: Int
var dates: [Date] = []
init(date: Date) {
let cal = Calendar.current
day = cal.component(.day, from: date)
month = cal.component(.month, from: date)
year = cal.component(.year, from: date)
dates.append(date)
}
func doesDateBelong(_ date: Date) -> Bool {
let cal = Calendar.current
return cal.component(.day, from: date) == day
&& cal.component(.month, from: date) == month
&& cal.component(.year, from: date) == year
}
}
这是用法:
func makeDate(year: Int, month: Int, day: Int) -> Date? {
let cal = Calendar.current
let components = DateComponents(year: year, month: month, day: day)
return cal.date(from: components)
}
var clusters: [DateCluster] = []
var dates = [
makeDate(year: 2020, month: 4, day: 20),
makeDate(year: 2020, month: 4, day: 19),
makeDate(year: 2020, month: 4, day: 19),
makeDate(year: 2020, month: 4, day: 18),
makeDate(year: 2020, month: 4, day: 18),
].compactMap { [=11=] }
for date in dates {
if let index = clusters.firstIndex(where: { [=11=].doesDateBelong(date) }) {
clusters[index].dates.append(date)
} else {
clusters.append(.init(date: date))
}
}
下面的结果将是所有按年月日分组的日期数组。
let groupedDates = clusters.map { [=12=].dates }
该代码从每个日期中提取年月日的日期分量并按它们分组,然后将分组结果中的值映射到每个日期的数组数组。为了能够在最终结果中包含原始日期,使用元组作为字典的值类型
let groupedDates = Dictionary(grouping: dates
.map { ([=10=],Calendar.current.dateComponents([.year, .month, .day], from: [=10=]))}, by: {[=10=].1})
.mapValues {value in value.map {[=10=].0}}
.values
我有一个 Date
对象数组,如下例所示:
let dates = [date1, date2, date3, date4, date5]
如何为具有相同 day of month
、month
和 year
.
示例:
date1 = 20/04/2020
date2 = 19/04/2020
date3 = 19/04/2020
date4 = 18/04/2020
date5 = 18/04/2020
...
有了这个,我想要以下内容:
let group1 = [date1]
let group2 = [date2, date3]
let group3 = [date4, date5]
let group4 = //...
我尝试了 DateComponents
和 .map
、.filter
等功能,但不知何故我无法做到这一点。
我能得到提示吗?
谢谢。
只需创建跟踪年月日的结构。
struct DateCluster {
let day: Int
let month: Int
let year: Int
var dates: [Date] = []
init(date: Date) {
let cal = Calendar.current
day = cal.component(.day, from: date)
month = cal.component(.month, from: date)
year = cal.component(.year, from: date)
dates.append(date)
}
func doesDateBelong(_ date: Date) -> Bool {
let cal = Calendar.current
return cal.component(.day, from: date) == day
&& cal.component(.month, from: date) == month
&& cal.component(.year, from: date) == year
}
}
这是用法:
func makeDate(year: Int, month: Int, day: Int) -> Date? {
let cal = Calendar.current
let components = DateComponents(year: year, month: month, day: day)
return cal.date(from: components)
}
var clusters: [DateCluster] = []
var dates = [
makeDate(year: 2020, month: 4, day: 20),
makeDate(year: 2020, month: 4, day: 19),
makeDate(year: 2020, month: 4, day: 19),
makeDate(year: 2020, month: 4, day: 18),
makeDate(year: 2020, month: 4, day: 18),
].compactMap { [=11=] }
for date in dates {
if let index = clusters.firstIndex(where: { [=11=].doesDateBelong(date) }) {
clusters[index].dates.append(date)
} else {
clusters.append(.init(date: date))
}
}
下面的结果将是所有按年月日分组的日期数组。
let groupedDates = clusters.map { [=12=].dates }
该代码从每个日期中提取年月日的日期分量并按它们分组,然后将分组结果中的值映射到每个日期的数组数组。为了能够在最终结果中包含原始日期,使用元组作为字典的值类型
let groupedDates = Dictionary(grouping: dates
.map { ([=10=],Calendar.current.dateComponents([.year, .month, .day], from: [=10=]))}, by: {[=10=].1})
.mapValues {value in value.map {[=10=].0}}
.values