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

437
Vistas
array.reduce() but need to set a key for the return value

I have an array where I am using .reduce to group all the matching ids of the array into new array with the id.

The problem is that my output looks like this


[
  {
    CHb5591f5db2a546d3af: [ [Object], [Object] ],
    CH5e86016c8b894d9d87: [ [Object], [Object], [Object], [Object], [Object], [Object] ]
  }
]

I need to have the output like this

[
  
    {id: CHb5591f5db2a546d3af, content: [ [Object], [Object] ]},
    {id: CH5e86016c8b894d9d87, content: [ [Object], [Object], [Object], [Object], [Object], [Object] ]}
  
]

Here is my attempt at the reducer function

result = await response.reduce(function (a, i) {
        a[i.messagesId] = a[i.messagesId] || []; //it generates a new property i.messagesId with an array if it does not exist (the value of not existing properties is undefined). if exist, it assign itself 
        a[i.messagesId].push(i);
        return a;
    }, Object.create({}));
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If there is single object inside array then you can easily achieve using Object.entries()

For demo purposes, I've used "object" as a string

const arr = [
  {
    CHb5591f5db2a546d3af: [["Object"], ["Object"]],
    CH5e86016c8b894d9d87: [
      ["Object"],
      ["Object"],
      ["Object"],
      ["Object"],
      ["Object"],
      ["Object"],
    ],
  },
];

const result = Object.entries(arr[0]).map(([id, content]) => ({ id, content }));
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

May be this is what you're looking for. We have a Array.prototype.reduce() method that returns our desired object array.

const array = [
      {
        'CHb5591f5db2a546d3af':
          [
            { animal: 'Cat', name: 'Tom' },
            { animal: 'Dog', name: 'Bruno' }
          ]
      },
      {
        'CH5e86016c8b894d9d87':
          [
            { animal: 'Alligator', name: 'Mike' }
          ]
      },
      {
        'CH5e86016c8b894d9d97':
          undefined
      }
    ]

    const reducer = function (acc, item) {
      for (const _key in item) {
        acc.push({
          'id': _key,
          'content': item[_key]
        })
      }
      return acc;
    }
    console.log(array.reduce(reducer, []))

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