Call it via:
const flat = flatten( realDeepObject ),
flatDotted = flatten( realDeepObject, '', '.' );
Test case:
const realDeepObject = {
level1: {
level2: {
level3: {
more: 'stuff', //duplicate key
other: 'stuff',
level4: {
the: 'end',
},
},
},
level2still: {
last: 'one',
},
am: 'bored',
},
more: 'stuff', //duplicate key
ipsum: {
lorem: 'latin',
},
};
const flat = flatten( realDeepObject ),
flatDotted = flatten( realDeepObject, '', '.' );
console.log( flat, flatDotted );
Output:
{ '/level1/level2/level3/more': 'stuff',
'/level1/level2/level3/other': 'stuff',
'/level1/level2/level3/level4/the': 'end',
'/level1/level2still/last': 'one',
'/level1/am': 'bored',
'/more': 'stuff',
'/ipsum/lorem': 'latin' }
{ 'level1.level2.level3.more': 'stuff',
'level1.level2.level3.other': 'stuff',
'level1.level2.level3.level4.the': 'end',
'level1.level2still.last': 'one',
'level1.am': 'bored',
'more': 'stuff',
'ipsum.lorem': 'latin' }