2024-07-19 13:09:29 +03:00
|
|
|
# flattenObject
|
|
|
|
|
|
|
|
Flattens a nested object into a single-level object with dot-separated keys.
|
|
|
|
|
|
|
|
- `Array`s are flattened.
|
|
|
|
- Non-plain objects, like `Buffer`s or `TypedArray`s, are not flattened.
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
function flattenObject(object: object): Record<string, any>;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Parameters
|
|
|
|
|
|
|
|
- `object` (`object`): The object to flatten.
|
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
|
|
|
(`T`): The flattened object.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
const nestedObject = {
|
|
|
|
a: {
|
|
|
|
b: {
|
2024-08-11 04:54:13 +03:00
|
|
|
c: 1,
|
|
|
|
},
|
2024-07-19 13:09:29 +03:00
|
|
|
},
|
2024-08-11 04:54:13 +03:00
|
|
|
d: [2, 3],
|
2024-07-19 13:09:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const flattened = flattenObject(nestedObject);
|
2024-08-11 04:54:13 +03:00
|
|
|
console.log(flattened);
|
2024-07-19 13:09:29 +03:00
|
|
|
// Output:
|
|
|
|
// {
|
|
|
|
// 'a.b.c': 1,
|
|
|
|
// 'd.0': 2,
|
|
|
|
// 'd.1': 3
|
|
|
|
// }
|
|
|
|
```
|