Need help understanding how to copy an object

I’m doing the Mysterious Organism exercise within the lesson about Objects and there is one bit I’m just not getting.

I create an object that has certain contents.
I copy the source object to a target object.
I make one slight change to the target objection.
I compare for differences.

The problem is I’m not copying the object, I’m simply creating a new variable that addresses the original object. Effective, after I’ve “copied” it, it now has two names.

I’ve attempted to include a screenshot and I hope that worked.

The values in the screenshot are from the quokka.js extension to Visual Studio Code.

To reiterate the steps above, but with more specific details:

  1. I create org1 via the object factory function pAequorFactory().
  2. Via console.log, I display the contents of org1.dna.
  3. I make a copy of org1 to org2
  4. Via console.log, I display the contents of org2.dna.
  5. I use the mutate() method to change one letter within org2.dna.
  6. Via console.log, I display the mutated contents of org2.dna.
  7. To confirm all is working right, I display the contents of org1.dna.

As you can see, when I use the mutate() method to change one letter within org2.dna, I do in fact change one letter.

The problem is, I also change the same one letter in org1, which indicates to me that org2 and org1 are two different references for the same object, not two different instances of similar objects.

I think what I need to do is create org2 as a true copy of org1, but I thought the Object.assign() method would do that.

Apparently, it didn’t.

Can anyone point me in the right direction?

Is that a recursive deep copy that updates a new object? We cannot assign objects that contain other objects. Only share references.

a = {}
b = a
b['a'] = 'b'
console.log(a['a'])
//  <- b

This parallels the behavior you are seeing. Next, we will look at this differently. Ponder on it for a moment and ping us when you’re ready to explore your question further.

Ok. So within this exercise, I’m to compare the DNA of different organisms, where the DNA is represented by an array of 15 DNA letters.

I’m thinking the way to do this is to define a series of arrays, one for each “organism”, where I simply store the DNA (which is randomly created) in these arrays.

Does this make sense?

You can use the slice method to copy an array.

newArray = oldArray.slice();

Can we have a look at your object factory function, please? As well, mockUpStrand() function.

I had to remove some console.log messages as this system was interpreting them as links and telling me that as a new user I’m limited to two.

Below is a link to the project.
https://www.codecademy.com/practice/projects/mysterious-organism

Below that is my current code. I’m going to try to finish the exercise now, if I can, and I’m very interested in your ideas.

// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = [‘A’, ‘T’, ‘C’, ‘G’]
return dnaBases[Math.floor(Math.random() * 4)]
}

// Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand =
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}

const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate () {
let randomDnaSlot = Math.floor(Math.random(15) * 15)
let randomBase = returnRandBase()
while (randomBase === dna[randomDnaSlot]) {
randomBase = returnRandBase()
}
dna[randomDnaSlot] = randomBase
return(this.dna)
}
}
}

let org1 = pAequorFactory(1, mockUpStrand())
console.log(org1.dna)
let org2 = Object.assign({},org1)
//let org2 = org1.assign
console.log(org2.dna)
org2.dna = org2.mutate()
console.log(org2.dna)
console.log(org1.dna)

I think I just found part of my problem. For .mutate() to change one value in a 15 slot array, it has to know which array in which to make the change, which requires I pass in an argument.

random does not take an argument.

I am not seeing what I’m doing wrong. I’ll remove the argument from .random(). Inside the object, I have copies of the original array, and the modified array that differ by one array location. Nothing I do allows me to have two arrays outside the object in which I can store a before and after. Once I change the one array position, the before is changed as well as the after.

I initially misunderstood what you meant. I understand now. That argument is being removed.

1 Like

I got it, but ■■■■ that was painful.

The secret to success was to clone the array as follows:
const orgTemp = […org1object.dna]

This created a new array which was a copy of the original array, such that when I changed the value in one array position, it changed it in the array I specified, and not both, as in this instance, they are in fact two separate arrays, not two references to the same array.

One tough lesson to learn, but now you know.

Most definitely! I doubt I will ever forget that copying objects is not as straight forward as it looks.

1 Like