Go to repository

Function flatten

Flatten the given object. Given an object with nested elements, it returns an object that has been flattened, that is, where the keys are string that represent the nested route to follow in the original object to access the leaves.

flatten({
a: {
a1: 1,
a2: 2
},
b: {
b1: {
b1i: 'hello',
b1ii: 'world'
}
}
});
// this will yield:
{
'a.a1': 1,
'a.a2': 2,
'b.b1.b1i': 'hello',
'b.b1.b1ii': 'world'
}

Additional options may be passed, such as which character is used as a delimiter, or the maximum depth level.

For an inverse operation see unflatten.

Type Parameters

  • TTarget extends Record<string, unknown>
  • TResult extends Record<string, unknown>

Parameters

Returns TResult

The flattened object