KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: New .flat() function in the Collection Class
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: August 1, 2023

As of 4D v20, the .flat() function can be called on a Collection. The .flat() function creates a new collection with all sub-collection elements concatenated into it recursively up to the specified depth.

Notes:
   •   By default, if the depth parameter is omitted, only the first level of the nested collection structure will be flattened.
   •   This function does not modify the original collection

Example:

$col:=New collection(1; 2; New collection(3; 4))
$test:=$col.flat()
// [1, 2, 3, 4]

$col1:=New collection(1; 2; New collection(3; 4; New collection(5; 6)))
$test1:=$col1.flat()
// [1, 2, 3, 4, [5, 6]]

$col2:=New collection(1; 2; New collection(3; 4; New collection(5; 6)))
$test2:=$col2.flat(2)
// [1, 2, 3, 4, 5, 6]

$col3:=New collection(1; 2; New collection(3; 4; 5; 6; New collection(7; 8; New collection(9; 10))))
$test3:=$col3.flat(MAXLONG)
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]