object-flat
v1.1.1
Published
flat nested object and return with accurate typing
Downloads
11
Maintainers
Readme
object-flat
🐤 flat nested object and return with accurate typing.
🥰 0 dependency.
⛲️ Out of box typescript support.
🦺 Tested.
Installation
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.
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
123 // number
null
undefined
'abc' // string
false
true
[]
new Number()
new Boolean()
() => {}
function () {}