Jump to content

I need to push a new object to the end of an array of objects..


Spunky

Recommended Posts

...Or something like that.

 

Here's what I got:

myObject = {  A:[{    name:"Letter A",    text:"I am the letter A"  }],  B:[{    name:"Letter B",    text:"I am the letter B"  }]}

The catch is I want to push a copy of one of those objects to the end of the object above, but not a complete copy since it will be called different, after the push it should look like this:

myObject = {  A:[{    name:"Letter A",    text:"I am the letter A"  }],  B:[{    name:"Letter B",    text:"I am the letter B"  }],  C:[{    name:"Letter B",    text:"I am the letter B"  }]}

How would I do this?

 

This is what I came up with so far:

lc = myObject.B;alert(lc);myObject.push({	C:lc})alert(myObject.C);

lc is indeed a copy of myObject.B, but I don't know how to get it in there, if it's possible. The error I get is that myObject.push is not a function. I just need "C" to output the same results as "B" when dynamically called, so maybe there is a different way set a replica?

Link to comment
Share on other sites

The error I get is that myObject.push is not a function.

Yeah, that's correct. Pushing is for arrays, not objects. You push an element onto the end of an array. Arrays are ordered, they are a collection of items in a list. Objects are not ordered, they are a collection of named properties. Just assign the variable to myObject.C. e.g.:
myObject.C = myObject.B;
However, that's not going to create a copy, it's going to create a link. Both B and C are now pointing to the same object, not different copies. You need to clone the object, there is a description about that here:http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object
Link to comment
Share on other sites

Oh yea I seen how complicated it was to clone objects the last time I thought I needed a clone. Turns out it was as simple as equalling one object to the other. However, for some reason since this object contains objects with properties or whatever it is doing, I didn't make the connection to the other and didn't realize it is still that simple, just needed the period there.

 

Summary: It does what I need it to do now. Thank you.

Link to comment
Share on other sites

Turns out it was as simple as equalling one object to the other. However, for some reason since this object contains objects with properties or whatever it is doing, I didn't make the connection to the other and didn't realize it is still that simple, just needed the period there.

 

Just make sure you read his post carefully though

 

Just assign the variable to myObject.C. e.g.:

myObject.C = myObject.B;
However, that's not going to create a copy, it's going to create a link. Both B and C are now pointing to the same object, not different copies. You need to clone the object, there is a description about that here:http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object

 

 

So unless you want to changes to B to propagate to C, then you have to use clone.

Link to comment
Share on other sites

To be explicit:

test.A = {msg: 'test 1'};test.B = test.A;test.B.msg = 'test 2';alert(test.A.msg); // shows "test 2", not "test 1", because they are the same object
If that's not the behavior you want then you need to clone the object, that's why clone exists. Doing this:
test.B = test.A;
Does not clone the object, it does not create a second copy. You just have 2 variables pointing to the same object, 2 ways to refer to the same thing.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...