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

157
Vistas
Remove duplicates from an array of objects but concatenate duplicate key

I've an array of objects that looks like this (a sample) -

let arrTable = [
{ tableName: "name 1", tableDesc:"desc 1", tableFooter:"footer 1"},
{ tableName: "name 2", tableDesc:"desc 2", tableFooter:"footer 2"},
{ tableName: "name 1", tableDesc:"desc 1", tableFooter:"footer 3"},
{ tableName: "name 3", tableDesc:"desc 3", tableFooter:"footer 4"},
{ tableName: "name 4", tableDesc:"desc 4", tableFooter:"footer 5"},
{ tableName: "name 5", tableDesc:"desc 5", tableFooter:"footer 6"},
{ tableName: "name 6", tableDesc:"desc 6", tableFooter:"footer 7"},
];

I want to remove duplicates with the same tableName but concatenate the tableFooter for those duplicates in the one that won't be removed.

For example, the expected array of objects should be -

    let newTable = [
    { tableName: "name 1", tableDesc:"desc 1", tableFooter:"footer 1 footer 3"},
    { tableName: "name 2", tableDesc:"desc 2", tableFooter:"footer 2"},
    { tableName: "name 3", tableDesc:"desc 3", tableFooter:"footer 4"},
    { tableName: "name 4", tableDesc:"desc 4", tableFooter:"footer 5"},
    { tableName: "name 5", tableDesc:"desc 5", tableFooter:"footer 6"},
    { tableName: "name 6", tableDesc:"desc 6", tableFooter:"footer 7"},
    ];

I've checked other questions on how to remove duplicates but is there an easier way to concatenate a key without having to run a for loop and check for same tableName and create a new key that saves the tableFooter in one?

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

0

I'd suggest using Array.reduce() to get the desired output.

We create an object with a property for each tableName, and if it already exists we append the tableFooter value.

let arrTable = [
{ tableName: "name 1", tableDesc:"desc 1", tableFooter:"footer 1"},
{ tableName: "name 2", tableDesc:"desc 2", tableFooter:"footer 2"},
{ tableName: "name 1", tableDesc:"desc 1", tableFooter:"footer 3"},
{ tableName: "name 3", tableDesc:"desc 3", tableFooter:"footer 4"},
{ tableName: "name 4", tableDesc:"desc 4", tableFooter:"footer 5"},
{ tableName: "name 5", tableDesc:"desc 5", tableFooter:"footer 6"},
{ tableName: "name 6", tableDesc:"desc 6", tableFooter:"footer 7"},
];

let newTable = Object.values(arrTable.reduce((acc, { tableName, tableFooter, ...rest}) => { 
    if (!acc[tableName]) {
        acc[tableName] = { tableName, ...rest, tableFooter};
    } else {
        acc[tableName].tableFooter += ` ${tableFooter}`;
    }
    return acc;
}, {}))

console.log(newTable)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need another array where you can push the non-duplicate items while modifying the duplicates.

This seems to work:

let arrTable = [
  { tableName: "name 1", tableDesc:"desc 1", tableFooter:"footer 1"},
  { tableName: "name 2", tableDesc:"desc 2", tableFooter:"footer 2"},
  { tableName: "name 1", tableDesc:"desc 1", tableFooter:"footer 3"},
  { tableName: "name 3", tableDesc:"desc 3", tableFooter:"footer 4"},
  { tableName: "name 4", tableDesc:"desc 4", tableFooter:"footer 5"},
  { tableName: "name 5", tableDesc:"desc 5", tableFooter:"footer 6"},
  { tableName: "name 6", tableDesc:"desc 6", tableFooter:"footer 7"},
];


let result = [];

arrTable.forEach((entry) => {
  // if the entry already exists, we just update its footer...
  // if you still need the original, make sure to make a copy of the entry here
  let existingEntry = result.find((e) => entry.tableName === e.tableName);
  if (existingEntry) {
    existingEntry.tableFooter += entry.tableFooter;
  } else {
    // new entries are added as they are
    result.push(entry);
  }
});

console.log('RESULT', result);
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