I have a large JS object which I feed to a factory. The original object contains strings in an array which are a parameter for the factory.
I would like to shorten the code to as short as possible and hopefully be functional.
What I have so far:
const myConfigObject = {
label: 'something',
children: [
{
id: 'one',
style: 'some style',
children: ['key1', 'key2']
},
{
id: 'two',
style: 'some other style',
children: ['key3', 'key4']
},
]
}
function DummyFactory (key) {
return {
id: key,
data: 'generated stuff'
};
}
// How to optimize this call?
myConfigObject.children.forEach(child => {
child.children = child.children.map(subChild => DummyFactory(subChild))
});
console.log(myConfigObject);