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

234
Vistas
How do i change the value of a key of a nested object in javascript

i have an object variable with nested keys like this

const obj = {
  kitchen: {
    painting: 100,
    piano: 1000,
    signature: "",
  },
  bathroom: {
    stereo: 220,
    signature: "",
  },
  signature: "",
};

i want to created a function that changes the value for the key "signature" with a name in both the root obj and any nested object that has a key "signature".

so:

function addSignature( obj , name){
}

returns

newObj = {
  kitchen: {
    painting: 100,
    piano: 1000,
    signature: name,
  },
  bathroom: {
    stereo: 220,
    signature: name,
  },
  signature: name,
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

i just made this and it works but idk if it is too "hacky"

function addSignature(obj, name) {
  if (obj.signature !== undefined) {
    obj.signature = name;
  }
  Object.keys(obj).forEach((key) => {
    if (typeof obj[key] === "object") {
      addSignature(obj[key], name);
    }
    else if (obj[key].signature !== undefined) {
      obj[key].signature = name;
    }
  });
  
  return obj;
}
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