Given an array with null values, how would you return a new array without any nulls in Appian?
Given an array with null values, how would you return a new array without any nulls in Appian?
Answer
Use the reject function to filter out all null values from the array.
Code:
Output:

Explanation:
a!localVariables(...) creates a scope for local variables. Variables defined inside are available only within that scope, and the last argument is the value the expression returns.
local!data: { 1, null, 6, 8, null, 19, 30, null }defines a list of eight items, three of themnull.reject(fn!isnull, local!data)takes a predicate function and a list, runs the function on each item, and drops every item for which it returnstrue. Withisnull()as the predicate, every null item is removed.
Replacing null values with Zero in Appian:
Sometimes Interviewer may ask a follow up replace the "null" with default value, in your case let replace the "null" with default value - zero
Code:
Output:

Explanation:
a!forEach(local!data, if(a!isNullOrEmpty(fv!item), 0, fv!item)) loops over a list, runs an expression on each item, and returns a new list of the results.
What happens in the loop
a!forEachgoes through the items inlocal!dataone at a time, in order.- On each pass,
fv!itemholds the current item. if(a!isNullOrEmpty(fv!item), 0, fv!item)returns0when the item is null or empty, and the item itself otherwise.- When the last item is done,
a!forEachreturns all the results as a new list.
Step by step with { 1, null, 6, 8, null, 19, 30, null }
Pass |
| Null or empty? | Returned |
|---|---|---|---|
1 | 1 | No | 1 |
2 | null | Yes | 0 |
3 | 6 | No | 6 |
4 | 8 | No | 8 |
5 | null | Yes | 0 |
6 | 19 | No | 19 |
7 | 30 | No | 30 |
8 | null | Yes | 0 |
Result
The result has the same length and order as the original, with each null replaced by 0. local!data itself doesn’t change.
Try it yourself
Change the inputs or the expression and run it to see what it returns.
Independent evaluator: it approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms