how to remove all nulls in array and get the corresponding index of the null value

Hello Appian people,

local!arr:{1,2,null,3,null}

loca!val:{1,2,3,4,5}

I have a function that gets null indexes in an array local!arr then used that indexes to remove the value from the local!val array.

and also remove the value found in the local!arr.

 /*Get null indices */
local!getNullIndices: a!forEach(
items: local!arr,
expression: if(isnull(fv!item), fv!index, null())
),
local!removeNullIndices: remove(
local!getNullIndices,
where(
a!forEach(
items: local!getNullIndices,
expression: if(a!isNullOrEmpty(fv!item), true, false)
)
)
),

/* Removed found null in data item array */
local!data: remove(
local!arr,
where(
a!forEach(
items: local!arr,
expression: if(a!isNullOrEmpty(fv!item), true, false)
)
)
),

local!updatedVal:remove(

loca!val,

removeNullIndices

)

expected value for updatedVal is: {1,2,4}

Do you have any ideas to optimize this implementation? Thank you

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    This is done much more simply by building a new array and leaving out the blank members (forget the remove() function altogether please).

    a!flatten(
      a!forEach(
        ri!array,
        if(
          a!isNotNullOrEmpty(fv!item),
          fv!item,
          {}
        )
      )
    )

    Note: The above works without a!flatten() up until you have an instance where all items in the array are blank.  A quirk of the data a!forEach() returns is an array of empty sets is returned when they're all empty (but when any entry is nonempty, the empty sets are ignored from the results).  Wrapping the whole thing in a!flatten() corrects this.

Reply
  • 0
    Certified Lead Developer

    This is done much more simply by building a new array and leaving out the blank members (forget the remove() function altogether please).

    a!flatten(
      a!forEach(
        ri!array,
        if(
          a!isNotNullOrEmpty(fv!item),
          fv!item,
          {}
        )
      )
    )

    Note: The above works without a!flatten() up until you have an instance where all items in the array are blank.  A quirk of the data a!forEach() returns is an array of empty sets is returned when they're all empty (but when any entry is nonempty, the empty sets are ignored from the results).  Wrapping the whole thing in a!flatten() corrects this.

Children
No Data