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

186
Vistas
How to summarize object information in JavaScript

I am trying to put together an object where some of this information was covered.

const tests = [
    {
        id: 1,
        name: 'taro',
        designId: 1,
        designName: 'design1'
    },
    {
        id: 1,
        name: 'taro',
        designId: 2,
        designName: 'design2'
    },
    {
        id: 2,
        name: 'Bob',
        designId: 3,
        designName: 'design3'
    },
    {
        id: 2,
        name: 'Bob',
        designId: 4,
        designName: 'design4'
    },
];

I want to set the following expectations.

result = [
  {
    id: 1,
    name: 'taro',
    designs: [
        { designId: 1, designName: 'design1' },
        { designId: 2, designName: 'design2' }
    ]
  },
  {
    id: 2,
    name: 'Bob',
    designs: [
        { designId: 3, designName: 'design3' },
        { designId: 4, designName: 'design4' }
    ]
  }
]

I have tried using lodash groupby as something I have tried, but I am struggling because I can't get rid of the extra properties.

const result = _.chain(tests)
    .groupBy('id')
    .map((value, key) => ({ id: key, designs: value }))
    .value();
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

When you map the groups, map the value, and omit the id and name from each group:

const tests = [{"id":1,"name":"taro","designId":1,"designName":"design1"},{"id":1,"name":"taro","designId":2,"designName":"design2"}];

const result = _(tests)
    .groupBy('id')
    .map((value, id) => ({
      id,
      name: value[0].name,
      designs: value.map(o => _.omit(o, ['id', 'name']))
    }))
    .value();
    
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do this using reduce and a bit of object decomposition:

const tests = [
    {
        id: 1,
        name: 'taro',
        designId: 1,
        designName: 'design1'
    },
    {
        id: 1,
        name: 'taro',
        designId: 2,
        designName: 'design2'
    },
];

const result = Object.values(tests.reduce( (acc, {id,name,...rest}) => {
   if(!acc[id])
      acc[id] = {id,name,designs:[]};
   acc[id].designs.push( {...rest} )
   return acc;
},{}))

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can easily achieve the result using for..of loop

const tests = [
  {
    id: 1,
    name: "taro",
    designId: 1,
    designName: "design1",
  },
  {
    id: 1,
    name: "taro",
    designId: 2,
    designName: "design2",
  },
];

let result = {};
for (let { id, name, ...rest } of tests) {
  result.id
    ? result.designs.push(rest)
    : (result = { id, name, designs: [rest] });
}

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

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