I have a tree object which is an irregular tree which children's names and key values can change everytime I run my code. For example:
{
addressRouter: 192.168.0.1,
addresses:
{
address1: 'A',
},
{
address2: 'B',
},
{
ports: [
{
portA: 'C',
portB: null
},
}
route: 'D',
}
so the names: 'addressRouter', 'addresses', 'address1', etc and their keys are unpredictable but I need to convert the tree object in arrays with the following format:
addressRouter
addresses/address1
addresses/address2
addresses/ports/portA
addresses/ports/portB
route
and then have their keys next to them.
I have this function to construct the tree, which is correct:
const iterate = (obj, obj2) => {
Object.keys(obj).forEach(key => {
obj2[key] = obj[key];
if (typeof obj[key] === 'object') {
iterate(obj[key], obj2)
}
})
}
but after debugging, I realized it doesn't get all branches.