Approach to filter data after fetched from stored procedure

Want to know best approach to filter data after getting fetched from Stored procedure

  Discussion posts and replies are publicly visible

Top Replies

Parents
  • Like the other poster said, in general I recommend doing your filtering during your query because the database is much more efficient at filtering than running an expression. That being said, there are some cases where it's necessary to filter in an expression, so if you have a relatively small dataset then there are two methods I've typically used to filter on the fly:

    a!localVariables(
      local!cases: {
        a!map(
          id: 1,
          priority: "Low",
          status: "New",
          dateUpdated: today()
        ),
        a!map(
          id: 2,
          priority: "Medium",
          status: "Pending Response",
          dateUpdated: today() - 4
        ),
        a!map(
          id: 3,
          priority: "Low",
          status: "Closed",
          dateUpdated: today() - 2
        ),
        a!map(
          id: 4,
          priority: "High",
          status: "New",
          dateUpdated: today() - 1
        ),
        
      },
      
      /* Option 1*/
      a!forEach(
        items: local!cases,
        expression: if(
          fv!item.priority = "Low",
          fv!item,
          {}
        )
      ),
      
      /* Option 2 */
      index(
        local!cases,
        wherecontains(
          "Low",
          local!cases.priority
        ),
        {}
      )
    )

    Option 1 uses a!forEach() to do a comparison, so any logical statement can be used to "filter" the data. It works because a!forEach() implicitly flattens all lists, so if you provide an empty list as the response in the false case, those items are filtered out.

    Option 2 uses a combination of index() and wherecontains() that is most useful if you're specifically looking if a value exists. The where contains tells you which indexes contain that value, and then you return the corresponding indexes from your original dataset with the index() function.

Reply Children
No Data