flatten
Flattens a nested object into a single-level object whose keys are the dot-notation path to each leaf value. The inverse of unflatten.
Only plain objects are recursed into — arrays, Date, Map, RegExp,
class instances, and empty plain objects {} are kept as opaque leaf
values. This keeps flatten/unflatten a clean, invertible pair: arrays
can’t be losslessly told apart from plain objects once reduced to dotted
keys, so this implementation doesn’t attempt it.
Caveat shared by every dotted-path flattening scheme: a key that itself
contains a literal . is indistinguishable from real nesting once
flattened ({ 'a.b': 1 } and { a: { b: 1 } } both produce { 'a.b': 1 }).
Available since v3.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
obj | Record<string, unknown> | The object to flatten |
Returns
Section titled “Returns”Record<string, unknown> — A single-level object with dot-notation keys
Examples
Section titled “Examples”Flatten a nested config object
Section titled “Flatten a nested config object”Each key in the result is the full dot-notation path to a leaf value.
Arrays and special objects are kept as leaves
Section titled “Arrays and special objects are kept as leaves”Only plain objects are recursed into — arrays stay intact.
