I tried to improve code using reducing:
private getAllRegistryObjects(registry: RegistryGeneric) {
const registryLayerItemGeneric = [];
registry.RegistryLayers.forEach((layer) => {
layer.items.forEach((item) => {
registryLayerItemGeneric.push(item);
});
});
return registryLayerItemGeneric;
}
I have tried this:
return registry.RegistryLayers.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue.concat(currentValue.items.flat());
}, [])
But it returns me empty array
Your code works:
var registry = {RegistryLayers: [
{ items: ['some', 'item'] },
{ items: ['other', 'things'] }
]};
var res = registry.RegistryLayers.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue.concat(currentValue.items.flat());
}, []);
console.log(res)
says:
['some', 'item', 'other', 'things']
To reduce need to repeatedly extend/copy the array, you can use flatMap instead if you just want them all joined up:
return registry.RegisteryLayers.flatMap((x) => x.items.flat());