li-s
07/03/2020, 4:32 AMdata class Item(val completed: Boolean, val details: String? = null)
and a list:
val p = listOf(
Item(true, "one"),
Item(false, "two"),
Item(true, "three")
)
I need to transform this data (truncate the second value in the Item to return something like:
listOf(
Item(true, "o"),
Item(false, "t"),
Item(true, "t")
)
Im trying:
val p = listOf(
Item(true, "one"),
Item(false, "two"),
Item(true, "three")
)
println(p.map { (_,details) -> details?.take(1) })
but it returns: [o, t, t]
What’s the best way to do this in Kotlin?
Thank you in advanceallan.conda
07/03/2020, 4:35 AMitem -> item.copy(details = item.details?.take(1))
li-s
07/03/2020, 4:37 AMli-s
07/03/2020, 4:38 AMMichael de Kaste
07/03/2020, 8:40 AMMichael de Kaste
07/03/2020, 8:42 AMval
). This means that whatever input you show, for them you need to recreate the entire dataclass if you want it's data to be modified.
item -> item.copy(details = item.details?.take(1))
is the same as:
item -> Item(item.completed, item.details?.take(1))
as in, you are creating a new item, with modified data