以下是我存储在名为 arrayEV 的数组中的数据结构 我很确定我可以通过顶部数组中的eventname进行过滤。 但是我不确定如何通过子阵列[heatlane]中的东西来过滤数组,所以我只想在[heatlane]
中的某些条件满足条件时显示主arrayEVstruct events {
var eventnumber:Int
var eventname:String
var swimmers:[heatlane]?
}
struct heatlane{
var heat:Int
var lane:Int
var age:Int
var swimmer:String
var team:String
var seedTime:String
var finalsTime:String
}
答案 0 :(得分:2)
通过简单属性(如array (
0 => '2-0',
1 => '4-0',
2 => '4-1',
3 => '4-10',
4 => '4-11',
5 => '4-12',
6 => '4-13',
7 => '4-14',
8 => '4-15',
9 => '4-16',
10 => '4-2',
11 => '4-3',
12 => '4-4',
13 => '4-5',
14 => '4-6',
15 => '4-7',
16 => '4-8',
17 => '4-9',
18 => '5-0',
19 => '7-1',
20 => '8-0',
21 => '9-0',
)
)或更复杂的属性(如Int
或其他)进行过滤的方法没有区别。您应该只传递应该过滤元素的条件。
如果您想过滤事件并且只有这些事件的名称以“A”开头,那么您可以写下:
Array
如果你想过滤那些名字以“A”为前缀的游泳运动员你会写的事件:
let prefixedWithA = events.filter{ $0.eventname.hasPrefix("A") }
答案 1 :(得分:0)
使用Array.filter
..
var array: [Any] = []
var heatlaneArr: [heatlane] = []
array.append(heatlaneArr)
像这样过滤数组,检查数组的每个数组元素
let findHeatlaneArray = array.filter { (obj) -> Bool in
return ((obj as? [heatlane]) != nil)
}
答案 2 :(得分:0)
let resultFilter = arrayEV.filter { (events) -> Bool in
guard let swimmers = events.swimmers else { return false } // check swimmers not nil
let result = swimmers.filter({ $0.age == 1}) // age condition
return result.count == 0 ? false : true
}