title: kotlin map objects and filter from inner list
body: <p>I'm making a DB request and getting back a list of objects. Within this list of objects is another list, which I want to filter on. After filtering, I then want to map the remaining objects to my domain model.</p>
  <p>So I have my Database model:</p>
  <pre><code>data class TaskDatabaseModel(
  
      @PrimaryKey
      @ColumnInfo(name = &quot;id&quot;)
      var id: String,
  
      @ColumnInfo(name = &quot;task_categories&quot;)
      val categories: List&lt;TaskCategory&gt;,
  
      //Other fields
  )
  </code></pre>
  <p>And my domain model:</p>
  <pre><code>@Parcelize
  data class Task(
      val id: String,
      val taskCategories: List&lt;TaskCategory&gt; = listOf(TaskCategory(TaskCategoryType.GENERAL)),
      //Other fields
  ) : Parcelable
  </code></pre>
  <p>TaskCategory:</p>
  <pre><code>@Parcelize
  data class TaskCategory(val type: TaskCategoryType, val id: String? = null) : Parcelable
  </code></pre>
  <p>My function to get the list:</p>
  <pre><code>override fun getTasks(taskId: String): Flowable&lt;List&lt;Task&gt;&gt; {
      return db.getTasksDao().getAllTasks()
          .map { taskList -&gt;
              taskList.map { listTaskDbModel -&gt;
                  listTaskDbModel.mapToDomainModel(cache.getCurrentUserId())
              }
          }
          .filter { taskList -&gt;
              taskList.find { it.taskCategories.find { category -&gt; category.id == taskId } != null } != null
          }
  }
  </code></pre>
  <p>The filter code returns all items and doesn't filter.</p>
  <p>How do I change it so I only return the objects that have the required <code>id</code> in the categories list? Would it be easier to do it on the DB query? The list is stored as JSON.</p>
  <p>Thanks</p>