过滤数组并创建新对象

时间:2018-01-16 10:43:16

标签: arrays swift filter

假设这个数组:

var studends = [("Marc", 404), ("Paul", 450), ("Steve", 490), ("Jar", 500), ("Niels", 520), ("Marco", 540), ("Sacha", 560), ("Mike", 500), ("Ted", 520), ("Ammar", 540), ("Rolph", 560), ("Brian", 590), ("Jeff", 600), ("John", 650), ("Mickael", 890)]

如何过滤数组并根据音符分割创建新对象,如下所示:

object A +/- 10% of 500
object B +/- 20% of 500
object C +/- 30% of 500
object D +/- 40% of 500

1 个答案:

答案 0 :(得分:0)

这是我的尝试:

struct FirstRange { 
var name: String 
var note: Int 
} 

struct SecondRange { 
var name: String 
var note: Int 
} 

var firstRanges = [FirstRange]() 
var secondRanges = [SecondRange]() 

for studend in studends {
    if studend.1 >= 450 && studend.1 <= 550 {
        let firstRange = FirstRange(name: studend.0, note: studend.1)
        firstRanges.append(firstRange)
    } else if studend.1 >= 400 && studend.1 <= 600 {
        let secondRange = SecondRange(name: studend.0, note: studend.1)
        secondRanges.append(secondRange)
    }
}

根据笔记,如何将最高范围排除在最低范围之外。