Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
How to improve code using reduce instead two loops?

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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());
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda