UNPKG

codetrix

Version:

A lightweight lodash-style utility library

14 lines (13 loc) 388 B
/** * Flattens a nested array into a single-level array. * * @template T The type of array elements. * @param array The nested array to flatten. * @returns A new flattened array. * * @example * flatten([1, [2, [3, 4]], 5]); // [1, 2, [3, 4], 5] */ export function flatten(array) { return array.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []); }