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

73
Views
Clone object within object

I have this object:

var a={
    objectFoo:{member1:"x",member2:"y"}
}

I want to make it have an additional identical child:

{
     objectFoo:{member1:"x",member2:"y"}
    ,objectBar:{member1:"x",member2:"y"}
}

If I do an assigment:

a.objectBar=a.objectFoo

it creates an unwanted link.

The way I handle it now is:

a.objectBar=JSON.parse(JSON.stringify(a.objectFoo))

This works, but in my novice mind feels wrong. It's like I am instructing the machine to do complex parsing stuff instead of just moving data from one point to another. Is my thinking too low level for javascript? Or is there a better way to do this?

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!