Is it possible to build a `filter()` function for `IdentifiedArrayOf<Element>`?

Hi there,

I'm trying to build a filter functionality, which takes a query (String) from a search bar, should be able to filter the results in real time and refresh the UI. The results is stored in a IdentifiedArrayOf<SomeStruct> in the state. Filter should not only consider the id, but also need to consider whether title and subtitle contains the query.

The elements in the IdentifiedArrayOf looks like this:

struct SomeStruct: Hashable, Identifiable {
    let id: String
    let title: String
    let subtitle: String

Didn't find a way to filter the items identified array just like you can do array.filter{ ... } on a regular swift array. How I solved it was to have another regular tempItems array in the state, and filter operates on tempItems (so that I can leverage the .filter function), and every time tempItems changes, update items with the new value of tempItems. So that the UI binded with items can also be updated.

struct SomeState {
    var items: IdentifiedArrayOf<SomeStruct> = []
    var tempItems [SomeStruct] = []

Please suggest better solutions, since these 2 arrays do introduce duplicated data stored in state.

Tried to build an extension but the generic syntax doesn't seem work for IdentifiedArrayOf:

extension IdentifiedArrayOf<T> where T: Identifiable {
    func filter() -> IdentifiedArrayOf<T> {
         // filtering logic

IdentifiedArray has a filter (source) method defined that returns an IdentifiedArray. Does that help?

Terms of Service

Privacy Policy

Cookie Policy