gpt4 book ai didi

ios - 如何去除过滤后的数组中的重复项?

转载 作者:行者123 更新时间:2023-11-29 01:11:34 26 4
gpt4 key购买 nike

我正在尝试获取一周的数组,但取而代之的是月份而不是日期或日期,因此包括今天在内的一周将是 ["Feb", "Feb", "Mar", "Mar", "Mar", "Mar", "Mar"],但我只希望数组在发生变化时显示,所以我希望它像 ["Feb","", "三月", "", "", "", ""]

var month = [String]()//this includes the months for the whole week
var filterdmonth = ["", "", "", "", "", "", ""]
for(var s = 0; s < month.count; s++){
if(month[s] != filterdmonth[0]){
filterdmonth[s] = month[s]
print(filterdmonth)
}

}

这会打印出 ["Feb", "", "Mar","Mar","Mar","Mar","Mar",]

最佳答案

我不确定您输入的是什么,但是如果 month["Feb", "Feb", "Mar", "Mar", "Mar", "Mar ", "Mar"],然后这是我开始工作的一些简单的东西。

let months = ["Feb", "Feb", "Mar", "Mar", "Mar", "Mar", "Mar"]
var workingMonth = ""

let filteredMonths = months.map { month -> String in
let result = month != workingMonth ? month : ""

if month != workingMonth {
workingMonth = month // There is a new month, so update the working month.
}

return result
}

print(filteredMonths)

关于ios - 如何去除过滤后的数组中的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35691220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com