WISHY
WISHY

Reputation: 11999

Extract list of objects from object list in kotlin?

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.

Upvotes: 3

Views: 2949

Answers (1)

Anand
Anand

Reputation: 927

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()

Upvotes: 4

Related Questions