Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use map function on condition in kotlin

Tags:

kotlin

I have a list of items and I want to edit its values before using it. I am using the map function to update each item in it. But the catch here is, I want to only update the items when the list size is 1. I want to return the list as it is if the size is larger than 1. How can I achieve this?

myList.map {
      if(resources.getBoolean(R.bool.is_tablet) && it.itemList.size<6 && it.layerType == DOUBLE_LIST) {
        it.layerType = SINGLE_LIST_AUTO
        it.itemList.forEach {sectionItem->
          sectionItem.layerType = SINGLE_LIST_AUTO
        }
        it
      }else{
        it
      }
    }
like image 645
sagar suri Avatar asked Aug 25 '26 23:08

sagar suri


1 Answers

You can try using filter before map:

.filter { it.itemList.size == 1 }
like image 89
Alex Avatar answered Aug 28 '26 01:08

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!