0

I am filtering an array based on a set filter supplied to the view (this filter varies) but want to modify the function to allow for an empty filter, i.e return the original array un filtered. I feel like this should be a simple matter but I'm currently a bit lost - have looked at the .isEmpty modifier and a couple of different IF statements but I'm not "getting it"

    var categoryFilter:String

var filteredCategoryTasks: [Task] {
    modelData.tasks.filter { task in
        (task.category == categoryFilter)
}
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94

1 Answers1

0

You could return the original array in the event that the String is empty and the filtered array otherwise:

var filteredCategoryTasks: [Task] {
    categoryFilter.isEmpty ? modelData.tasks : modelData.tasks.filter { task in
          (task.category == categoryFilter)
    }
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94