Extract list of objects from object list in kotlin?

Viewed 812

I have a list of actions object. I am trying to make it a list of string on basis of action type

Sample data

{
"wicked": "not at all",
"footer": {
"actions": [
  {
    "icon": "",
    "type": "click"
  },
  {
    "icon": "",
    "type": "click"
  },
  {
    "icon": "",
    "type": "click"
  }
]
}
}

These are my data classes

data class Footer(
val actions: List<Actions>?)

data class Actions(
val copy: String?,
val action: String?)

This is the code I am trying to extract string list of action types using map operator

dataFooter?.actions?.map { it.type }?.toCollection()

I am not sure what should go in the toCollection method.

1 Answers

Use toList()

dataFooter?.actions?.map { it.action }?.toList()

If you need a list of string on the unique value of the type then use toSet()

Related