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

223
Vistas
javascript group a nested array of objects by child object value

Im trying to group an array of objects by a value of one of the child objects.

Im kinda getting want I want using reduce, but it seems to be combining the group by value where the parent objects are common.

let name = [
  {
    issue: "89",
    status: ["test", "prod", "dev"]
  },
  {
    issue: "45",
    status: ["dev"]
  }
];

const groups = name.reduce((groups, item) => {
  const group = groups[item.status] || [];
  group.push(item);
  groups[item.status] = group;
  return groups;
}, {});

console.log(JSON. stringify(groups));

I get the below results

{
   "test,prod,dev":[
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ],
   "dev":[
      {
         "issue":"45",
         "status":[
            "dev"
         ]
      }
   ]
}

What id like is for it the produce the below:

{
   "prod":[
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ],
   "test":[
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ],
   "dev":[
      {
         "issue":"45",
         "status":[
            "dev"
         ]
      },
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ]
}

Im not sure how to produce my desired results easily.

many thanks

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

const group = Object.assign({}, ...name.reduce((p, c) => [...p, ...c.status], [])
.filter((v, i , a) => a.indexOf(v) === i)
.map(item => ({ [item]: name.filter(old => old.status.includes(item)) })))
about 4 years ago · Santiago Trujillo Denunciar

0

You need to group your object based on the status. You can use array#reduce with array#forEach.

const names = [ { issue: "89", status: ["test", "prod", "dev"] }, { issue: "45", status: ["dev"] } ],
      result = names.reduce((r, o) => {
        o.status.forEach(name => {
          r[name] ??= [];
          r[name].push(JSON.parse(JSON.stringify(o)));
        });
        return r;
      },{});
console.log(result);

about 4 years ago · Santiago Trujillo 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