2

Is there a way I can duplicate a Object in JavaScript but then have different values in the duplicate?

For example if the original Object is as follows:

    a           = [];
    a[0]        = {};
    a[0].milk   = "Malted";
    a[1]        = {};
    a[1].milk   = "Goats";

I then want to copy the Object

    b = a;
    b[0].milk = "Semi";

If I use clone() or assign the previous 'a' Object is also altered so if I wrote

document.write(a[0].milk+" * "+b[0].milk);

The output is:

Semi * Semi where I am expecting Malted, Semi

Thanks in advance.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Flash20
  • 73
  • 4

0 Answers0