使用kotlin中的筛选操作从Parent中删除子元素



我想通过删除不匹配的子项来过滤嵌套的列表数据。你可以在这里看到科特林游乐场的例子https://pl.kotl.in/b-6SOvMvZ

val filterTxt = "an"
val filterData = mainData.deviceList.filter { items -> 
items.itemList.any { 
it.itemName.contains(filterTxt, true)
}
}

当前正在使用带有筛选器的any,但由于它返回所有项,因此未得到预期结果。

Current Result : [[fan, mobile], [bottle, mobile, fan]]
Expected Result : [[fan], fan]]
如果列表中的任何给定元素满足谓词,则

any将返回true。这不是你想要的。您只需要满足条件(或谓词(的元素。

val filterData = mainData.deviceList.map { items -> 
items.itemList.filter { 
it.itemName.contains(filterTxt, true)
}
}

相关内容

最新更新