arr 은 임의의 숫자를 전달받은 초기화값입니다.
map 을 통해서 데이터를 가공한 students = [Student] 배열을 만들고,
이를 다시 filter 를 통해서 적합한 데이터만 남기도록 할 수 있습니다.
struct TestScore { let korean: Int let english: Int let math : Int } struct Student{ let name: String let score: Int } var arr = [TestScore](repeating: TestScore(korean: Int.random(in: 1...100), english: Int.random(in: 1...100), math: Int.random(in: 1...100)), count: 0) for _ in 1 ... 5 { arr.append(TestScore(korean: Int.random(in: 1...100), english: Int.random(in: 1...100), math: Int.random(in: 1...100))) } var students = arr.enumerated().map{ (index: Int, score: TestScore) -> Student in Student(name: String(index), score: Int((Double(score.korean) + Double(score.english) + Double(score.math)) / 3)) }.filter { (student: Student) -> Bool in student.score >= 70 } print(students)
comment