Use of Filter function

Can someone explain the best use of filter function?

  Discussion posts and replies are publicly visible

Parents Reply
  • Personally I almost never use the filter() function. I use its inverse reject() more often because it allows you to easily remove nulls from a list:

    a!localVariables(
      local!test: {1, null, 3, null, 7},
      reject(
        fn!isnull,
        local!test
      )
    )

    As far as implementing behavior for filtering, I often use a method very similar to what you mentioned. The where function lets you define a boolean for each item in the list, so it makes it easier to use in a variety of scenarios. For example, suppose I want to return all the items with a cost over 100:

    a!localVariables(
      local!data:{
        {
          id: 3,
          name: "Item 1",
          cost: 50
        },
        {
          id: 2,
          name: "Item 2",
          cost: 120
        },
        {
          id: 4,
          name: "Item 3",
          cost: 75
        },
        {
          id: 7,
          name: "Item 4",
          cost: 150
        },
      },
      index(
        local!data,
        where(
          tointeger(local!data.cost)>100
        ),
        {}
      )
    )

    If you want more complex logic, you can replace line 27 with whatever expression you want that returns a boolean and it should allow you to filter the data as needed.

Children
No Data