1

I have a problem.

I have a simple object like this:

var simpleObject = {

   name = "John",
   favColor = "blue
}

and another object with more properties and objects inside:

I want to put a COPY of simpleObject inside the bigger object like this:

biggerObject.copyofSimpleObject = simpleObject;

but what happens is whenever i change the value of simpleObject the value of biggerObject.copyofSimpleObject also changes.

So it seems like it's a reference to the original object which is a cool thing to do but in this situation I need it to be a copy so that when the original simpleObject changes the biggerObject.copyofSimpleObject value stays the same. I know it's probably simple but I don't know how to do this because I'm a noob. Please help :) Thank you! This is all javascript I'm using.

Ivelin
  • 139
  • 1
  • 11

1 Answers1

0

You can simply do:

var bigObject = {copyObject:simpleObject}

And define your simpleObject like this:

var simpleObject = {
   name : "John",
   favColor :"blue"
}

And then you can access it using:

bigObject.simpleObject
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108