Reputation: 83
I want to filter nested lists with kotlin without changing the object type.
data class ExamResponse(
val list: List<ExamObj>
)
data class ExamObj(
val objList: List<ExamObj2>
)
data class ExamObj2(
val name: String,
val age: Int
)
For example, I want to get the list 'ExamObj' with age value 27 for the above model.
The method to return the list is as follows.
fun progress(respList: List<ExamObj>): List<ExamObj>{}
this method takes a list of 'ExamObj' and filters the 'objList' in 'ExamObj' and returns the 'ExamObj' list again
val result = respList.map {
it.objList.filter {
it.age == 27
}
}
Using this I achieved the desired result but the type issue appeared.
Upvotes: 0
Views: 820
Reputation: 71
I think you forgot to wrap filtered ExamObj2
list in a ExamObj
class...
val result = respList.map { examObj ->
ExamObj(examObj.objList.filter { it.age == 27 })
}
Upvotes: 2