3

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.

2
  • toCollection() is accepting another list where you want to add mapped elements with map. Commented May 30, 2021 at 15:19
  • Why do you need to run toCollection() or toList() in the first place? dataFooter?.actions?.map { it.type } already returns a list of strings. Commented May 30, 2021 at 18:33

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.