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

229
Vistas
JS - Combine Multiple Values in Object Into New Key in Same object

This should be simple, but I am stumbling on it.

What I have:

{
 notSet: 2,
 set: 9,
 blanks: 4,
 other: 8
}

Into:

{
  newKey: 6, (combine notSet and blanks + remove them)
  set: 9,
  other: 8
 }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you can do this

const obj = {
 notSet: 2,
 set: 9,
 blanks: 4,
 other: 8
}

const transform = ({notSet, blanks, ...rest}) => ({
  ...rest,
  newKey: notSet + blanks
})

const newObject = transform(obj)

console.log(newObject)

about 4 years ago · Juan Pablo Isaza Denunciar

0

With delete keyword and a few checks it is possible. You would want to delete your extra keys.

I used the ?? (nullish coalescing operator) because some keys might not exist, and safer to use a 0 value, otherwise you might get NaN (if you try to add null/undefined with a number):

const obj1 = {
 notSet: 2,
 set: 9,
 blanks: 4,
 other: 8
};

const transformObj = (obj1) => {
obj1.newKey = (obj1.notSet ?? 0) + (obj1.blanks ?? 0);
delete obj1.notSet;
delete obj1.blanks;
};

transformObj(obj1);
console.log(obj1);

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