How to remove a object from a data class in kotlin

How to remove a object from a data class in kotlin

Problem Description:

I want to remove a object from the list and so that i can add just required string and pass it . i have a model class like this

data class TagItem(
  val tagTittle: String,
  val isSelected: Boolean
)

this data class is mapped for the Lazy Column for making the list select and deSelect the items

 var tagsItems by remember {
                  mutableStateOf(
                      (tagsList).map {
                          TagItem(
                              tagTittle = it,
                              isSelected = false
                          )
                      }
                  )
              }

              val productEveryTags = tagsItems.filter {
                    it.isSelected
                }

                Log.i(TAG,"Only this $productEveryTags ")

                viewModel.onEvent(ProductUploadEvent.EnteredProductTags(productEveryTags))

i am filtering the selected items alone but in my log statement

Only this [TagItem(tagTittle=Tagged , isSelected=true), TagItem(tagTittle=Ducati , isSelected=true)]

How can i remove the "isSelected" object and just get the "tagTittle" alone into a single List

Solution – 1

You can simply map your instances for the output:

Log.i(TAG,"Only this ${productEveryTags.map { it.tagTittle }}")

Or combine it with the existing filter. Depending on whether you are interested in duplicates, you can also directly map to a set:

val productEveryTags = tagsItems.filter {
        it.isSelected
    }.mapTo(LinkedHashSet()) {
        it.tagTittle
    }

Log.i(TAG,"Only this $productEveryTags")
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject