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

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

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