Copies of nested arrays and references in javascript
<p>The following javascript code [<a href="http://jsfiddle.net/AlexZeitler/U4cpa/4/" rel="nofollow">jsfiddle</a>]:</p>

<pre><code>Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from &lt; 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

var masterlist = [{
    name: "Master1"},
{
    name: "Master2"}];
var parentlist = [{
    name: "Parent1"},
{
    name: "Parent2"}];
var childlist = [{
    name: "Child1"},
{
    name: "Child2"}];

for (var i = 0; i &lt; masterlist.length; i++) {
    var master = masterlist[i];
    master.parents = parentlist.slice();
    for (var j = 0; j &lt; master.parents.length; j++) {
        var parent = master.parents[j];
        parent.children = childlist.slice();
    }
}

console.log("before removing")
console.log("master1 parents: " + masterlist[0].parents.length);
console.log("master2 parents: " + masterlist[1].parents.length);
console.log("master1 parent 1 childrens: " + masterlist[0].parents[0].children.length);
console.log("master2 parent 2 childrens: " + masterlist[1].parents[1].children.length);

masterlist[0].parents.remove(0);
masterlist[0].parents[0].children.remove(0);

console.log("after removing")
console.log("master1 parents: " + masterlist[0].parents.length);
console.log("master2 parents: " + masterlist[1].parents.length);
console.log("master1 parent 1 childrens: " + masterlist[0].parents[0].children.length);
console.log("master2 parent 2 childrens: " + masterlist[1].parents[1].children.length);?
</code></pre>

<p>results in:</p>

<blockquote>
before removing<br/>
master1 parents: 2<br/>
master2 parents: 2<br/>
master1 parent 1 childrens: 2<br/>
master2 parent 2 childrens: 2<br/>
after removing<br/>
master1 parents: 1<br/>
master2 parents: 2<br/>
master1 parent 1 childrens: 1<br/>
master2 parent 2 childrens: 1<br/>
</blockquote>

<p>I would expect (and need) this:</p>

<blockquote>
before removing<br/>
master1 parents: 2<br/>
master2 parents: 2<br/>
master1 parent 1 childrens: 2<br/>
master2 parent 2 childrens: 2<br/>
after removing<br/>
master1 parents: 1<br/>
master2 parents: 2<br/>
master1 parent 1 childrens: 1<br/>
master2 parent 2 childrens: 2 &lt;-- difference <br/>
</blockquote>

<p>What am I doing wrong?
It looks like the childs are referring to the same array also I did slice the initial children array (which did work with the parent array as expected).</p>
