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

261
Vistas
How to remove duplicates from array of objects and combine values of that duplicates?

I have array like this, my id and name will be same for multiple objects but organizations values can be different

array= [
  {id: 1, name: "Test1", organizations: 1},
  {id: 1, name: "Test1", organizations: 2},
  {id: 1, name: "Test1", organizations: 3},
  {id: 2, name: "Test2", organizations: 4},
  {id: 2, name: "Test2", organizations: 5},
  {id: 2, name: "Test2", organizations: 6} 
];

I want to convert this to be like this:

expectedArray =  [
  {id: 1, name: "Test1", organizations: [1,2,3]},
  {id: 2, name: "Test2", organizations: [4,5,6]} 
];

Can someone please help

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

0

const array= [
  {id: 1, name: "Test1", organizations: 1},
  {id: 1, name: "Test1", organizations: 2},
  {id: 1, name: "Test1", organizations: 3},
  {id: 2, name: "Test2", organizations: 4},
  {id: 2, name: "Test2", organizations: 5},
  {id: 2, name: "Test2", organizations: 6} 
];

const mergeDuplicates= (field, uniqueField) => (source = [], value)=> {
    const target = source.find( item => item[uniqueField] == value[uniqueField] );
    if(target) target[field].push( value[field] );
    else source.push({...value, [field]: [ value[field] ] });
    return source;
}
const mergeOrganaizationsById = mergeDuplicates('organizations','id')
const result = array.reduce(mergeOrganaizationsById, [])
console.log(result)

about 4 years ago · Santiago Trujillo Denunciar

0

You'll want to reduce.

array.reduce((acc, cur) => {
  const i = acc.findIndex(a => a.id === cur.id && a.name === cur.name);
  if (i === -1) return [...acc, {...cur, organizations: [cur.organizations]}];
  return [...acc.slice(0, i), {...cur, organizations: [...acc[i].organizations, cur.organizations]}, ...acc.slice(i +1)];
}, []);
about 4 years ago · Santiago Trujillo Denunciar

0

You can achieve the output using forEach by grouping based on name and then pushing the necessary fields into the output array.

const array = [
  { id: 1, name: "Test1", organizations: 1 },
  { id: 1, name: "Test1", organizations: 2 },
  { id: 1, name: "Test1", organizations: 3 },
  { id: 2, name: "Test2", organizations: 4 },
  { id: 2, name: "Test2", organizations: 5 },
  { id: 2, name: "Test2", organizations: 6 }
];

const current = Object.create(null);
const finalArr = [];
array.forEach(function (o) {
  if (!current[o.name]) {
   current[o.name] = [];
   finalArr.push({ id: o.id, name: o.name, organizations: current[o.name] });
  }
  current[o.name].push(o.organizations);
});
console.log(finalArr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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