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

169
Vistas
How to grouped values in object?

I wrote a function that allows you to group values in an object, but it doesn't work quite right

This is my varation

const response = Object.values(
      footerMenu.reduce((acc, key) => {
        const { databaseId, parentDatabaseId } = key;
        acc[parentDatabaseId] = (acc[parentDatabaseId] || []).concat(key);
        return acc;
      }, {})
    );

example data:

[{parentDatabase: 0, databaseId: 5728}, {parentDatabseId: 5728, databaseId: 5762}....{parentDatabse: 0, databaseId: 4532}, {parentDatabaseId: 4532, databaseId: 3221} ...]

how to group an item so that parent Database Id == 0 has its own items that relate to it

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

0

you have to perform a simple manipulation with the array — group the items by parentDatabaseId

The usual way is by invoking the array.reduce() method with the right callback function:

let array=[{parentDatabase: 0, databaseId: 5728}, {parentDatabase: 5728, databaseId: 5762},{parentDatabase: 0, databaseId: 4532}, {parentDatabase: 4532, databaseId: 3221} ]

const groupByDbId = array.reduce((group, item) => {
  const { parentDatabase } = item;
  group[parentDatabase] = group[parentDatabase] ?? [];
  group[parentDatabase].push(item);
  return group;
}, {});

console.log(JSON.stringify(groupByDbId, null, 2)); 

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