How to remove keys from the list of map that has null values

Certified Associate Developer

Hi, 

I want to remove the null value keys from the list of the map below. Please let me know how to achieve that:

{a!map(ABC: 6.7, EFG: 15.44, HIJ: 470.888, KLM: "9.8989984", OPQ: null), a!map(ABC: 8.4, EFG: 15.43, HIJ: 470.656, KLM: "9.2342", OPQ: null), a!map(ABC: 7.2, EFG: 15.42, HIJ: 470.554, KLM: "9.234234", OPQ: null), a!map(ABC: 5.6, EFG: 15.42, HIJ: 470.234, KLM: "9.63242", OPQ: null), a!map(ABC: "", EFG: 15.42, HIJ: 470.665, KLM: "9.54343", OPQ: null)}

Regards,

Sowvik

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    I have tried to replicate your requirement; please have a look at it.

    a!localVariables(
      local!originalList: {
        a!map(ABC: 6.7, EFG: 15.44, HIJ: 470.888, KLM: "9.8989984", OPQ: null),
        a!map(ABC: 8.4, EFG: 15.43, HIJ: 470.656, KLM: "9.2342", OPQ: null),
        a!map(ABC: 7.2, EFG: 15.42, HIJ: 470.554, KLM: "9.234234", OPQ: null),
        a!map(ABC: 5.6, EFG: 15.42, HIJ: 470.234, KLM: "9.63242", OPQ: null),
        a!map(ABC: "", EFG: 15.42, HIJ: 470.665, KLM: "9.54343", OPQ: null)
      },
    
      a!forEach(
        items: local!originalList,
        expression: a!localVariables(
          local!currentMap: fv!item,
          local!nullKeys: reject(
            fn!isnull,
            a!forEach(
              items: a!keys(local!currentMap),
              expression: if(
                isnull(index(local!currentMap, fv!item, null)),
                fv!item,
                null
              )
            )
          ),
          remove(local!currentMap, local!nullKeys)
        )
      )
    )

  • 0
    Certified Lead Developer

    It's not exactly documented but it was recently pointed out that the "remove()" function, classically thought of as removing an "index" as in an integer position within an array, can also be pointed at a named property and will "remove" that property from the result it outputs.  I believe it's a TINY bit "use at your own risk" still, but as it's a primitive function (iirc) I wouldn't worry too much about its behavior being dramatically impacted by any updates anytime soon (esp considering any alternate approaches to the same require a great deal of workaround).

    Looks like  has already posted you a working solution (even IF he used "index()" where i'd insist someone use "property()" Innocent).

  • 0
    Certified Associate Developer

    This could also work:

    a!localVariables(
      local!data: {
        a!map(
          ABC: 6.7,
          EFG: 15.44,
          HIJ: 470.888,
          KLM: "9.8989984",
          OPQ: null
        ),
        a!map(
          ABC: 8.4,
          EFG: 15.43,
          HIJ: 470.656,
          KLM: "9.2342",
          OPQ: null
        ),
        a!map(
          ABC: 7.2,
          EFG: 15.42,
          HIJ: 470.554,
          KLM: "9.234234",
          OPQ: null
        ),
        a!map(
          ABC: 5.6,
          EFG: 15.42,
          HIJ: 470.234,
          KLM: "9.63242",
          OPQ: null
        ),
        a!map(
          ABC: "",
          EFG: 15.42,
          HIJ: 470.665,
          KLM: "9.54343",
          OPQ: null
        )
      },
      a!forEach(
        items: local!data,
        expression: a!localVariables(
          local!item: fv!item,
          local!keysToRemove: a!forEach(
            items: a!keys(local!item),
            expression: if(
              a!isNullOrEmpty(index(local!item, fv!item, null)),
              fv!item,
              {}
            )
          ),
          remove(local!item, local!keysToRemove)
        )
      )
    )