Skip to main content

Function: flatten()

Call Signature

flatten(obj, options?): FlatObject

Defined in: object/flatten/index.ts:45

Flattens a nested object into a single-level object with dot-notation keys.

Converts nested structures like { user: { name: 'Alice' } } into { 'user.name': 'Alice' }. Useful for form data, query parameters, and database operations.

Parameters

obj

PlainObject

The object to flatten

options?

FlattenOptions

Flattening options (separator, maxDepth, flattenArrays)

Returns

FlatObject

A flattened object with dot-notation keys

Example

// Data-first
const nested = { user: { name: 'Alice', age: 30 } }
flatten(nested)
// => { 'user.name': 'Alice', 'user.age': 30 }

// Custom separator
flatten(nested, { separator: '_' })
// => { 'user_name': 'Alice', 'user_age': 30 }

// Max depth
flatten({ a: { b: { c: 1 } } }, { maxDepth: 1 })
// => { 'a.b': { c: 1 } }

// Data-last (in pipe)
pipe(
nested,
flatten({ separator: '_' })
)

See

unflatten - for reversing the flattening

Call Signature

flatten(options?): (obj) => FlatObject

Defined in: object/flatten/index.ts:46

Flattens a nested object into a single-level object with dot-notation keys.

Converts nested structures like { user: { name: 'Alice' } } into { 'user.name': 'Alice' }. Useful for form data, query parameters, and database operations.

Parameters

options?

FlattenOptions

Flattening options (separator, maxDepth, flattenArrays)

Returns

A flattened object with dot-notation keys

(obj): FlatObject

Parameters

obj

PlainObject

Returns

FlatObject

Example

// Data-first
const nested = { user: { name: 'Alice', age: 30 } }
flatten(nested)
// => { 'user.name': 'Alice', 'user.age': 30 }

// Custom separator
flatten(nested, { separator: '_' })
// => { 'user_name': 'Alice', 'user_age': 30 }

// Max depth
flatten({ a: { b: { c: 1 } } }, { maxDepth: 1 })
// => { 'a.b': { c: 1 } }

// Data-last (in pipe)
pipe(
nested,
flatten({ separator: '_' })
)

See

unflatten - for reversing the flattening