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

196
Visualizações
Rename (add suffix to) every key except 2 in every object in array of objects javascript

We have the following javascript array:

[
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 }
]

and we are looking to add the suffix Agst to every key other than the team key, such that our output is:

[
  { team: 1, stat1Agst: 17, stat2Agst: 25, var1Agst: 'okay', var3Agst: 'nah', hereAgst: 24 },
  { team: 1, stat1Agst: 17, stat2Agst: 25, var1Agst: 'okay', var3Agst: 'nah', hereAgst: 24 },
  { team: 1, stat1Agst: 17, stat2Agst: 25, var1Agst: 'okay', var3Agst: 'nah', hereAgst: 24 }
]

Each object in our actual array of objects has >100 keys, and for this reason we are looking for a solution that specifically changes all key names other than team, rather than changing all keys with names ['stat1', 'stat2', 'var1', 'var3', 'here'] if possible.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can easily achieve the result using map and Object.entries as:

const arr = [
  { team: 1, stat1: 17, stat2: 25, var1: "okay", var3: "nah", here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: "okay", var3: "nah", here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: "okay", var3: "nah", here: 24 },
];

const result = arr.map((o) =>
  Object.fromEntries(
    Object.keys(o).map((key) =>
      key === "team" ? [key, o[key]] : [`${key}Agst`, o[key]]
    )
  )
);

console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

This seems to be working okay for me.

arr = [
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 }
]

arr.forEach((row, index, theArray) => {
    let rowKeys = Object.keys(row).filter(key => key !== 'team');
    rowKeys.forEach(key => {
        theArray[index][`${key}Agst`] = theArray[index][key];
        delete theArray[index][key];
    });
});

console.log(arr);

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