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

205
Views
Object.keys(obj).forEach making deep copy of object js

I need to write a function for deep copying of objects. I have wrote one, but it doesn't work. Could anyone help me to fix it? thanks in advance! const cloneObj = {};

    function makeObjectDeepCopy2(obj, cloneObj) {

        Object.keys(obj).forEach((key) => {
            if (obj[key] instanceof Object) {
                cloneObj[key] = makeObjectDeepCopy2(obj[key], cloneObj[key]);
            }
            cloneObj[key] = obj[key];
            return cloneObj;
        });
    }

    var a = {
        a: 5,
        b: { g: 8, y: 9, t: { q: 48 } },
        x: 47,
        l: { f: 85, p: { u: 89, m: 7 }, s: 71 },
        r: { h: 9, a: 'test', s: 'test2' }
    };

    makeObjectDeepCopy2(a, cloneObj);
    console.log(cloneObj);
    console.log(a);
about 4 years ago · Juan Pablo Isaza
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!