For the complete documentation index, see llms.txt. This page is also available as Markdown.

ArrayFlatten

Flattens nested arrays to the specified depth.

When depth is omitted, the array is flattened completely.

 nested = [ 1, [ 2, [ 3 ] ] ];
 nested.flatten(); // [ 1, 2, 3 ]
 nested.flatten( 1 ); // [ 1, 2, [ 3 ] ]
 

Method Signature

ArrayFlatten(array=[array], depth=[integer])

Arguments

Argument
Type
Required
Description
Default

array

array

true

The array to flatten.

depth

integer

false

The depth to flatten. If omitted, flatten all nested arrays.

Examples

Flatten a nested array completely

When no depth is specified, all levels of nesting are flattened.

nested = [ 1, [ 2, [ 3, [ 4 ] ] ] ];
result = nested.flatten();
writeOutput( result.toString() );

Result: [1, 2, 3, 4]

Flatten to a specific depth

Result: [1, 2, [3, [4]]]

Flatten two levels deep

Result: [1, 2, 3, [4]]

Using the global function form

Result: [a, b, c]

Last updated

Was this helpful?