Skip to main content

flatten

$flatten

Returns a new array with sub-array elements flattened to the specified depth. Throws if the input is not an array.

Parameters

  • depth (number, optional, default 1): The depth to flatten. Use Infinity to flatten completely.

Example

// Flatten one level of nesting
new Schema('array').transformer('$flatten')
// [[1, 2], [3, 4]] → [1, 2, 3, 4]

// Flatten all levels
new Schema('array').transformer({$flatten: {depth: Infinity}})
// [1, [2, [3, [4]]]] → [1, 2, 3, 4]

// Flatten exactly 2 levels
new Schema('array').transformer({$flatten: {depth: 2}})