Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda