Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!