Please observe the result of these forEach, (1. and 2.):
1.
a!forEach( items: {1,2,3,4,5}, expression: if ( fv!item > 3, fv!item, {} )
)
results in
meanwhile
2.
a!forEach( items: {1,2,3,4,5}, expression: if ( fv!item > 3, fv!item, null )
Question: Why forEach 1. does not outcome five members in the list?, I expected this result
Discussion posts and replies are publicly visible
This is the expected result. In your first example, the fallback value is set as an empty set (i.e. an array of size zero), which by standard Appian convention is excluded from array result sets (at least when the entire array contains any non-empty value). This is very useful when it comes to creating a trimmed array where the only members meet a certain condition, since the negative case can typically just be set to an empty set such as is seen here.
In your second example, on the other hand, the fallback value is a single null, which is very different from an empty set - a null is considered to be (in terms of data type) a single value, just with a blank value, but that doesn't stop its data type from being singular. This is often used for example to insert nulls into arrays (or replace items in an array with null) wherein it's important for the array to maintain its original size/shape, but for individual items to be excluded due to whatever reasons are given in the logic.
Further I'd clarify that this doesn't really have anything in particular to do with a!forEach(), but rather, the behavior of empty sets and nulls in arrays overall (since at the end of the day, a!forEach() just generates an array). A simple example shows this pretty clearly:
a!forEach() automatically flattens results.{} (Empty array) -> Gets flattened awaynull -> Get preserved
Hi Rafael,This is the expected result and this is a subtle behavior in a!forEach that often catches people off guard.Here’s the difference:
a!forEach
{} is treated as an empty list, so a!forEach skips that item entirely.
{}
null is treated as a value, so it's included in the output list.
null
That’s why in example 1, only 4 and 5 appear, the others were dropped because {} returns nothing. Using null ensures the full list structure is preserved.
Hope that helps!
Shubham Aware said:a!forEach() automatically flattens results.
There's actually an important corner case that would make me hesitant to put it this simply - since it specifically *doesn't* (exactly) flatten results - that is, when the result is all empty sets, it will return an array of empty sets (where the average user might want to have just gotten back an empty array). When this is a possibility, it's necessary to wrap the a!forEach() call in a!flatten() which actually does flatten the result.