I have a nested array of objects. I would like to get an object that only provides me the onClick property as the key and the value as null. I am able to navigate to the first level, but I am unable to navigate down further. How do I go forward doing it?
const headers = [{
id: 'title1',
title: 'Title 1',
children: [
{
title: 'Children 1',
child: [
{
title: 'Child 1',
id: 'child1Id',
onClick: 'child1Click',
url: '/child1'
},
{
title: 'Child 2',
id: 'child2Id',
onClick: 'child2Click'
}
]
},
{
title: 'Children 2',
child: [
{
title: 'Child 3',
id: 'child3Id',
onClick: 'child3Click',
},
{
title: 'Child 4',
id: 'child4Id',
onClick: 'child4Click'
}
]
}
]
},
{
id: 'title2',
title: 'Title 2',
privilege: '',
children: [{
title: 'Children 3',
privilege: '',
child: [{
title: 'Child 5',
id: 'child5Id',
onClick: 'child3Click',
url: '/child5',
privilege: ''
},
{
title: 'Child 6',
id: 'child6Id',
onClick: 'child6Click',
url: '/child6',
privilege: ''
}
]
},
{
title: 'Children 4',
privilege: '',
child: [{
title: 'Child 7',
id: 'child7Id',
onClick: 'child7Click',
url: '/child7',
privilege: ''
},
{
title: 'Child 8',
id: 'child8Id',
onClick: 'child8Click',
url: '/child8',
privilege: ''
}
]
}
]
}
];
const routesMap = ({ onClick, children }) => (onClick ? { [onClick]: null } : _.flatMap(children, routesMap));
const routeStates = _.assign({}, ..._.flatMap(headers, routesMap));
console.log(routeStates)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Expected Output:
{
child1Click: null,
child2Click: null,
child3Click: null,
child4Click: null,
child5Click: null,
child6Click: null,
child7Click: null,
child8Click: null,
}
Please advice. Any help is highly appreciated.