Call it via:
const flat = 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 );
console.log( flat );
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' }
(You can replace the separators /
with a dot .
to make it fit your job)
doesnt work with cyclic objects like
window