https://github.com/tylim88/object-flat
flat nested object and return with accurate typing
https://github.com/tylim88/object-flat
Last synced: about 2 months ago
JSON representation
flat nested object and return with accurate typing
- Host: GitHub
- URL: https://github.com/tylim88/object-flat
- Owner: tylim88
- License: mit
- Created: 2021-10-25T03:03:08.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-03T16:16:33.000Z (about 3 years ago)
- Last Synced: 2025-03-13T20:08:36.481Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 498 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# object-flat
[](https://www.npmjs.com/package/object-flat) [](https://github.com/tylim88/object-flat/blob/master/LICENSE) [](<[LINK](https://github.com/tylim88/object-flat#object-flat)>) [](https://www.npmjs.com/package/object-flat?activeTab=dependencies)
🐤 flat nested object and return with accurate typing.
🥰 0 dependency.
⛲️ Out of box typescript support.
🦺 Tested.
## Installation
```bash
npm i object-flat
```## 🎵 Usage
flatten nested object into flat object, you can define what character to join the nested key path.
NOTE: does not flatten array and what inside the array.
```ts
import flatten from 'object-flat'const flattenObj = flatten(
{ a: 1, b: { c: 3, d: { e: 4 } }, f: { g: { h: 'a', j: [{ a: 1 }] } } }, // target
'.' // character to join the key path.
)
// return { a: 1, 'b.c': 3, 'b.d.e': 4, 'f.g.h': 'a', 'f.g.j': [{ a: 1 }]},
// type is { a: number, 'b.c': number, 'b.d.e': number, 'f.g.h': string, 'f.g.j': { a: number }[]]}
```**NOTE: Flatten ONLY object literal, does NOT flatten below data type**
```ts
123 // number
null
undefined
'abc' // string
false
true
[]
new Number()
new Boolean()
() => {}
function () {}
```