Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Object = Object" problem for this new guy 1

Status
Not open for further replies.

JFanning

Technical User
Feb 22, 2002
4
US
I'm working on a set of webpages that contain a client-side Javascript based database of personal profiles. I need to be able to copy the data from one custom object to another, but can't find an efficient way to do this.

Setup:

I have created a custom array object "profile[x]" where 'x' is the person's ID. The profile has custom properties, for example:

profile[x].FirstName
profile[x].LastName
...etc, etc.

I need to be able to move all of the data from profile[x] to profile[y] where it can be editted without changing the data in profile[x]. I tried the logical solution:

profile[y] = profile[x]

However, this appears to create a permanent binding between the two instances of profile. So if I later do 'profile[y].FirstName = "Ed" ', profile[x].FirstName is ALSO changed to "Ed".

There is a rather large number of customer properties for profile, and I don't want to have to assign every single one of them individually. How can I copy all of the values from profile[x] to profile[y] without creating a permanent link between the two?

Thanks,
Joe
 
well, what you have to do is:
profile[y].Firstname = profile[x].Firstname
etc
you cannot just say object1 = object2
you have to say it in a format of:
object1.property1 = object2.property1

Also, may be you could show us the source code, it might help us help you! --------------------------------------------------
Goals are dreams with deadlines
 
I was afraid that's what I had to do... make the individual assignments for every propery. The problem is that there are about a hundred custom properties for 'profile' (which is why I didn't try to paste the entire code in the post!). I didn't want to put all those lines in my code and was hoping there was some sort of wildcard characater I could use:

profile[y].* = profile[x].*

Oh well. I guess the other option would be to create an array of profile properties:

array = ("LastName","FirstName", etc, etc... )

And then dynamically construct the assignment statement in a loop through all the properties. The code would look prettier, but the execution time may suffer.
 
though it will make the execution time slower, I do not think it will be THAT much slower if it is all client-side (which I assume it is).
I hope somebody else will appear here and prove me wrong! --------------------------------------------------
Goals are dreams with deadlines
 
Hi valeriav,

You could try this:

function copyProfile(sourceProfile, destinationProfile) {
for(var p in sourceProfile) {
destinationProfile[p] = sourceProfile[p];
}
}

copyProfile(profile[x], profile[y]);


This will copy each property of the first profile to the second one.

Greetz,

Raver1

 
Why not just do this:

Code:
profile[y]=profile[x].toString().split(",");

This code turns
Code:
profile[x]
into a string and then back into an Array through the
Code:
split()
method.
bluebrain.gif
blueuniment.gif
 
Hi UNIMENT,

Creating a string and then converting it to an Array again might work, but you won't be able to copy objects that way.

profile[x].picture = new Image();
profile[x].address = new addressObject(street, town, country);

Greetzz...

Raver1
 
Oh... I didn't realize that the
Code:
for...in
loop could do Object properties, too!

That's cool!
You get a star ;-)
bluebrain.gif
blueuniment.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top