我有一个像下面这样的模型
data class BaseModel(
val title: String,
val number: Int,
val items: List<BaseModel>? = null
)
并且该模型被生成为
fun generateModel(): List<BaseModel> {
val childItem1 = BaseModel("child1", 1)
val childItem2 = BaseModel("child2", 2)
val childItem3 = BaseModel("child3", 3)
val childItem4 = BaseModel("child4", 4)
val childItem5 = BaseModel("child5", 5)
val childItem6 = BaseModel("child6", 6)
val childItem7 = BaseModel("child7", 7)
val childItem8 = BaseModel("child8", 8)
val childList1 = arrayListOf(childItem1, childItem2, childItem3)
val childList2 = arrayListOf(childItem4, childItem5)
val childList3 = arrayListOf(childItem6, childItem7, childItem8)
val parentItem1 = BaseModel("parent1", 11, childList1)
val parentItem2 = BaseModel("parent2", 22, childList2)
val parentItem3 = BaseModel("parent3", 33, childList3)
return arrayListOf(parentItem1, parentItem2, parentItem3)
}
如何从该数组中过滤编号为7的BaseModel对象?[ title: "parent3", number: 33, items: [ title: "child7", number: 7, items: null ] ]
我尝试了许多方法使用Map和过滤器,但无法达到的结果。
1条答案
按热度按时间zpjtge221#
如果你需要使用像filter这样的常用函数,你必须使数组平面化。
你可以这样做: