a!flatten() Function

Converts an array that contains other arrays into an array of single items.

Syntax

a!flatten( array )

  • array (Any Type Array): Array to be flattened.

Returns

Any Type Array

Notes

a!flatten() removes nesting from arrays, such as those created by looping functions.

a!forEach(items: {1, 2, 3}, expression: enumerate(fv!item)) returns a 3 item list consisting of {0}, {0, 1}, and {0, 1, 2}

a!flatten(a!forEach(items: {1, 2, 3}, expression: enumerate(fv!item))) returns {0, 0, 1, 0, 1, 2}


When passed a simple array, a!flatten() returns it unmodified:

a!flatten({1, 2, 3}) returns {1, 2, 3}


Nulls are not removed from lists.

a!flatten(merge({1, null}, {"a", "b"})) returns {1, "a", null, "b"}

You can use reject() and isnull() to easily remove nulls from a flattened list.


a!flatten() always returns an array, so when passed a single item, a!flatten() returns an array containing that item:

a!flatten(1) returns {1}


When passed an empty array, a!flatten() returns an empty array. Likewise, when passed null, it returns an empty array.

a!flatten({}) returns {}

a!flatten(null) returns {}


Only array nesting is removed. Nested maps, dictionaries, and data types retain their structure.

1
2
3
4
5
6
7
a!flatten(
  {
    { id: 1 },
    { id: 2 },
    { id: 3, address: { street: "Main Street", number: 101 } }
  }
)

returns:

1
2
3
4
5
{
  { id: 1 },
  { id: 2 },
  { id: 3, address: { street: "Main Street", number: 101 } }
}

If the passed array is type Any Type, a!flatten() returns an array of the appropriate type.

typename(typeof(a!flatten({a: {1, 2, 3}}.a))) returns "List of Number (Integer)"

typename(typeof(a!flatten({a: {1, 2, "apple"}}.a))) returns "List of Variant"

FEEDBACK