Traté de mejorar el código usando la reducción:
private getAllRegistryObjects(registry: RegistryGeneric) { const registryLayerItemGeneric = []; registry.RegistryLayers.forEach((layer) => { layer.items.forEach((item) => { registryLayerItemGeneric.push(item); }); }); return registryLayerItemGeneric; }He intentado esto:
return registry.RegistryLayers.reduce(function(previousValue, currentValue, currentIndex, array) { return previousValue.concat(currentValue.items.flat()); }, [])Pero me devuelve una matriz vacía.
Tu código funciona:
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)dice:
['some', 'item', 'other', 'things'] Para reducir la necesidad de extender/copiar repetidamente la matriz, puede usar flatMap en su lugar si solo quiere que todos se unan:
return registry.RegisteryLayers.flatMap((x) => x.items.flat());