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

144
Vistas
Convert an array of object to a new array of object with keys in javscript

I have an array of objects in the format below and would like to transform it into a new array of objects using a property as a key. The key should be unique. See shape of the object below

const mockedList = [
  {
    email: 'aaa@example.com',
    id: '5052',
    name: 'Java',
  },
  {
    email: 'bbb@example.com',
    id: '5053',
    name: 'Python',
  },
  {
    email: 'aaa@example.com',
    id: '5054',
    name: 'C#',
  },
  {
    email: 'bbb@example.com',
    id: '5055',
    name: 'Javascript',
  },
];

I would like to transform this and get an array of objects with keys and values in this format.

[
   {
      email: 'bbb@example.com',
      languages: [
         {
            email: 'bbb@example.com',
            id: '5055',
            name: 'Javascript',
         },
         {
            email: 'bbb@example.com',
            id: '5053',
            name: 'Python',
         },
     ]             
   },
   {
      email: 'aaa@example.com',
      languages: [
         {
            email: 'aaa@example.com',
            id: '5052',
            name: 'Java',
         },
        {
            email: 'aaa@example.com',
            id: '5054',
            name: 'C#',
        },
     ]
   }
]

I've tried using map-reduce

const result = mockedList.reduce((r, a) => {
  r[a.email] = r[a.email] || [];
  r[a.email].push(a);
  return r;
}, Object.create(null));

But did not get the right shape of data

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

0

You can do:

const mockedList = [{email: 'aaa@example.com',id: '5052',name: 'Java',},{email: 'bbb@example.com',id: '5053',name: 'Python',},{email: 'aaa@example.com',id: '5054',name: 'C#',},{ email: 'bbb@example.com', id: '5055', name: 'Javascript' },]

const mockedListHash = mockedList.reduce((a, c) => {
  a[c.email] = a[c.email] || { email: c.email, languages: [] }
  a[c.email].languages.push(c)
  return a
}, {})
const result = Object.values(mockedListHash)

console.log(result)

In case you want to clean the repeated emails within languages:

const mockedList = [{email: 'aaa@example.com',id: '5052',name: 'Java',},{email: 'bbb@example.com',id: '5053',name: 'Python',},{email: 'aaa@example.com',id: '5054',name: 'C#',},{ email: 'bbb@example.com', id: '5055', name: 'Javascript' },]

const mockedListHash = mockedList.reduce((a, c) => {
  a[c.email] = a[c.email] || { email: c.email, languages: [] }
  a[c.email].languages.push({
    id: c.id,
    name: c.name,
  })
  return a
}, {})
const result = Object.values(mockedListHash)

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is another option with simple for loop

// Array
const mockedList = [
  {
    email: 'aaa@example.com',
    id: '5052',
    name: 'Java'
  },
  {
    email: 'bbb@example.com',
    id: '5053',
    name: 'Python'
  },
  {
    email: 'aaa@example.com',
    id: '5054',
    name: 'C#'
  },
  {
    email: 'bbb@example.com',
    id: '5055',
    name: 'Javascript'
  }
];

// Set new object
const newObj = {};

// Use regular loop
for(const el of mockedList) {
  // Use email as key
  // If key already exist, add info
  // to it's languages array
  if(newObj[el.email]) newObj[el.email].languages.push(el);
  else newObj[el.email] = {
    email: el.email,
    languages: [el]
  }
}

// Test
console.log(newObj);

// If you need just array of objects,
// without email as key, then transform it
const newArr = Object.keys(newObj).map((key) => newObj[key]);

// Test
console.log(newArr);

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