Hi guys, how can I refactor that code? ```val prod...
# codereview
i
Hi guys, how can I refactor that code?
Copy code
val productShopInfoItems = productListItemMapper.toShopInfoItems(it.snapshot)

state.productList.value?.forEach { product ->
    productShopInfoItems.forEach {  shop ->
        if(product.productId == shop.productId){
            product.shopInfoItemList += shop
        }
    }
}
product is
Copy code
data class ProductItem(
    val productId: String = "",
    val productName: String = "",
    var shopInfoItemList: List<ShopInfoResponseItem> = emptyList<ShopInfoResponseItem>()
)
r
Copy code
shopsByProductId = productShopInfoItems.groupBy { it.productId }
state.productList.value?.forEach { it.shopInfoItemList = shopsByProductId[it.productId] ?: emptyList() }
groupBy is your friend (creates a map of
key -> list of every item where f(item) == key
)
1
👍 1
i
Thank you