Flatten your objects and arrays by a delimiter
$ npm install --save falcon-punch
Not using Node or a module bundler? Use a UMD build via the <script>
tag.
- https://unpkg.com/falcon-punch/dist/falcon-punch.js
- https://unpkg.com/falcon-punch/dist/falcon-punch.min.js
import falconPunch from 'falcon-punch'
const o = {
a: 1,
b: {
c: 'falcon',
d: {
e: 'punch'
}
}
}
// { a: 1, 'b_c': 'falcon', 'b_d_e': 'punch' }
falconPunch(o)
// Specify opts: delimiter & maxDepth
// { a: 1, 'b???c': 'falcon', 'b???d': { e: 'punch' } }
falconPunch(o, { delimiter: '???', maxDepth: 1 })
// go crazy
const arr = [
{
a: [
[
{
b: 0,
c: 1
},
10
],
{
d: {
e: null,
f: [],
g: {},
h: undefined
}
}
]
},
[[1, [{}]]]
]
/*
{
'0.a.0.0.b': 0,
'0.a.0.0.c': 1,
'0.a.0.1': 10,
'0.a.1.d.e': null,
'0.a.1.d.f': [],
'0.a.1.d.g': {},
'0.a.1.d.h': undefined,
'1.0.0': 1,
'1.0.1.0': {}
}
*/
falconPunch(arr, { delimiter: '.' })