Array
The Array utilities are implemented as a single-function module that exports the flatMap function for recursive array flattening operations.
flatMap
The flatMap function implements recursive flattening of nested object arrays with full TypeScript type safety:
export const flatMap = <
T extends Record<string, any>,
K extends Extract<keyof T, string>
>(
resource: T[],
key: K
): Array<Omit<T, K>>
Parameters:
- resource: Array of objects containing nested arrays
- key: String key identifying the property containing nested arrays
Returns: Flattened array with the specified key omitted from all objects
Input Structure Example:
Live Editor
function () { const resource = [ { id: 1, name: "Item 1", children: [ { id: 2, name: "Item 1.1", children: [] }, { id: 3, name: "Item 1.2", children: [{ id: 4, name: "Item 1.2.1", children: [] }], }, ], }, ]; const result = UtilityWrapper.flatMap(resource, "children"); return ( <> <div>Result: </div> <div>{JSON.stringify(result)}</div> </> ); }
Result
Loading...
Flexible Key Support
The function supports any string key name for nested arrays:
// Works with "children" key
flatMap(treeData, "children");
// Works with "nodes" key
flatMap(nodeData, "nodes");
// Works with any custom key
flatMap(customData, "subcategories");