ios - Swift: Best way to filter an array -
i have array student objects , array goodstrudentid. need fetch student objects students array following sequence of goodstrudentid. using multiple forloop can able solve this, want learn best way solve issue. problem following sequence of goodstrudentid. here sample code understand problem -
class student { var s_id : string var s_name : string init(i: string, n: string) { self.s_id = i; self.s_name = n; } } var students = [student(i: "1",n: "a"), student(i: "2",n: "b"), student(i: "3",n: "c"), student(i: "4",n: "d"), student(i: "5",n: "e")] var goodstudentsid = ["5","2"] var goodstudentobject = getgoodstudentobjectusingid(students:students, gdstudentsid:goodstudentsid) /* expected answer: var goodstudentobject = [student(i: "5",n: "e"), student(i: "2",n: "b")] */ func getgoodstudentobjectusingid(students:array<student>, gdstudentsid:array<string>) -> array<student>! { /*?????? please complete func*/ return []; }
func getgoodstudentobjectusingid(students:[student], gdstudentsid:[string]) -> [student] { return students.filter { gdstudentsid.contains($0.s_id) } }
Comments
Post a Comment