title: How can I copy a variable without pointing to the same object?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object">What is the most efficient way to clone a JavaScript object?</a> (Most of these use external libraries, or don't work completely. The Blog Post below is a much better resource)</p>
  </blockquote>
  
  
  
  <p>This is in the context of the scribd developer challenge. Their API allows me to call get_board and get the board. But, what I want to do is have a copy of that board that I modify in set_field. But, when I do that it messes with their own internal board object and breaks their code. So, What I need (I think) is to call get_board and then break the pointer to the object and have my own board I modify. I've attempted this below with mygame.board but I still get the same error.</p>
  
  <p>The error reads: <code>The type of an object is incompatible with the expected type of the parameter associated to the object</code></p>
  
  <pre><code>function new_game()
  {
      var board = get_board();
      var mygame={board: board}
      mygame.board = set_field(min_max_store, mygame.board)
  }
  
  function set_field(min_max_store, board)
  {
  
    //loop x
     //loop y
      board[x][y] = -1;
  return board
  }
  </code></pre>
  
  <p>How can I stop the pointer from pointing to their object and get my own to modify?</p>
  
  <p><strong><em>EDIT:</em></strong></p>
  
  <p>This will work for 2d arrays.</p>
  
  <pre><code>var one = [[1,2],[2,3],[3,4],[4,5]]
  var two = one.slice(0)
  
  for(var x = 0; x &lt;= one.length - 1; x++)
  {
    two[x] = one[x].slice(0)
  }
  
  one[1][1] = 10
  
  console.log(one)
  console.log(two)
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>[[1, 2], [2, 10], [3, 4], [4, 5]]
  [[1, 2], [2, 3], [3, 4], [4, 5]]
  </code></pre>