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

190
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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