UNPKG

@andranik-arakelyan/js-utilities

Version:
38 lines (37 loc) 1.25 kB
/** * Flattens an array to a specified depth, converting nested arrays into a single-level array. * * @template T - The type of elements in the array * @param array - The array to flatten * @param depth - The depth level specifying how deep a nested array structure should be flattened (default: 1) * @returns A new flattened array * * @example * // Flatten one level deep (default) * const nested = [1, [2, 3], [4, [5, 6]]]; * const result = flatten(nested); * console.log(result); // [1, 2, 3, 4, [5, 6]] * * @example * // Flatten completely (infinite depth) * const deepNested = [1, [2, [3, [4, 5]]]]; * const result = flatten(deepNested, Infinity); * console.log(result); // [1, 2, 3, 4, 5] * * @example * // Flatten to specific depth * const mixed = [1, [2, [3, [4, 5]]]]; * const result = flatten(mixed, 2); * console.log(result); // [1, 2, 3, [4, 5]] * * @example * // With objects and mixed types * const data = [ * { id: 1 }, * [{ id: 2 }, { id: 3 }], * [{ id: 4 }, [{ id: 5 }]] * ]; * const result = flatten(data); * console.log(result); // [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, [{ id: 5 }]] */ export declare function flatten<T>(array: readonly (T | readonly any[])[], depth?: number): T[];