Toolbox-XToolbox-X

Flatten Object Key Value

Recursively flattens a nested object structure into a single-level object while preserving leaf values.

flattenObjectKeyValue

The flattenObjectKeyValue function recursively flattens a nested object structure into a single-level object containing all leaf properties.

Function Signature

function flattenObjectKeyValue<T extends GenericObject>(
  object: T
): FlattenLeafValue<MergeAll<[T]>>

Parameters

  • object (T): The nested object to flatten.

Return Value

  • FlattenLeafValue<MergeAll<[T]>>: A flat object with leaf values merged to the top level.

Example Usage

playground.ts

Notes

  • Key Collisions: If nested levels contain properties with duplicate keys, the value from the deepest nested property will override the shallower keys.
  • Arrays: Arrays are treated as terminal/leaf values and are not flattened.
  • Returns a new object without mutating the original object.

Comparison with flattenObjectDotNotation

  • flattenObjectKeyValue: Merges keys directly to the root, discarding structural paths (e.g. { name: 'John' }).
  • flattenObjectDotNotation: Encodes structural paths into dot-separated keys (e.g. { 'user.details.name': 'John' }).

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page