Array/Since
1.38.0flattenDeep
Recursively flattens nested arrays of any depth into a single array.
flattenDeep(array):T[]Parameters
arrayunknown[]requiredThe array containing values and potentially nested arrays to flatten.
Returns
T[]A new array containing all non-array values from the input array.
Examples
01
Basic usage
Flatten nested arrays into a single array.
TypeScript
import { flattenDeep } from "@cyref/js";
const values = [1, [2, 3], [4, [5, 6]]];
const result = flattenDeep<number>(values);
console.log(result);
// [1, 2, 3, 4, 5, 6]02
Multiple levels of nesting
The function recursively flattens arrays regardless of their nesting depth.
TypeScript
import { flattenDeep } from "@cyref/js";
const values = [
1,
[2, [3, [4, [5]]]],
];
const result = flattenDeep<number>(values);
console.log(result);
// [1, 2, 3, 4, 5]03
With strings
Nested arrays containing strings are recursively flattened into a single array.
TypeScript
import { flattenDeep } from "@cyref/js";
const values = [
["JavaScript", ["TypeScript"]],
[["Python", "Go"]],
];
const result = flattenDeep<string>(values);
console.log(result);
// ["JavaScript", "TypeScript", "Python", "Go"]04
Empty arrays
Empty arrays are ignored and do not add elements to the result.
TypeScript
import { flattenDeep } from "@cyref/js";
const values = [
[],
[1, [2]],
[],
[[3]],
];
const result = flattenDeep<number>(values);
console.log(result);
// [1, 2, 3]Built with@cyref/js·Official documentation