Skip to main content
August 8, 2024
Solved

how to merge two arrays excluding null values

  • August 8, 2024
  • 7 replies
  • 0 views

hi, 

I have two arrays,{"","1"} and {"2","3"}  

I want to merge them, but exclude null and have something like this 

{"1","2","3}.

Any help is appreciated 

    Best answer by stefanhelzle0001

    filter(a!isNotNullOrEmpty(_), union({"","1"}, {"2","3"}))

    7 replies

    stefanhelzle0001
    August 8, 2024

    filter(a!isNotNullOrEmpty(_), union({"","1"}, {"2","3"}))

    August 8, 2024

    just curious to know, what does this return if both arrays does not have null values 

    stefanhelzle0001
    August 8, 2024

    Just give it a try ...

    mikes0011
    Brainy
    August 8, 2024

    For an implementation that avoids the old and clunky primitive looping functions like filter(), we can use a!forEach() in this (and almost all other) use cases:

    a!localVariables(
      local!array1: {"", "1"},
      local!array2: {"2", "3"},
    
      a!flatten(
        a!forEach(
          union(local!array1, local!array2),
          if(
            a!isNotNullOrEmpty(fv!item),
            fv!item,
            {}
          )
        )
      )
    )

    August 8, 2024

    I will try this

    August 8, 2024

    tried this, it worked, thank you

    prasantap0900
    August 9, 2024

    a!localVariables(
    local!a: { "", "1" },
    local!b: { "2", "3" },
    a!forEach(
    items:{local!a,local!b},
    expression: if(isnull(fv!item),{}, fv!item)
    )
    )