Skip to main content
Brainy
Aug 8, 2024 3:46 PM UTC
Solved

how to merge two arrays excluding null values

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 

    stefanhelzle0001
    Brainy
    Aug 8, 2024 3:49 PM UTC

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

    7 replies

    stefanhelzle0001
    Brainy
    Aug 8, 2024 3:49 PM UTC

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

    Brainy
    Aug 8, 2024 4:16 PM UTC

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

    stefanhelzle0001
    Brainy
    Aug 8, 2024 4:52 PM UTC

    Just give it a try ...

    mikes0011
    Brainy
    Aug 8, 2024 5:00 PM UTC

    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,
            {}
          )
        )
      )
    )

    The Expression Guru
    Brainy
    Aug 8, 2024 6:33 PM UTC

    I will try this

    Brainy
    Aug 8, 2024 7:00 PM UTC

    tried this, it worked, thank you

    prasantap0900
    Participating Frequently
    Aug 9, 2024 7:12 AM UTC

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