Possible Duplicate:
What is the most efficient way to clone a JavaScript object? (Most of these use external libraries, or don't work completely. The Blog Post below is a much better resource)
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.
The error reads: The type of an object is incompatible with the expected type of the parameter associated to the object
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
}
How can I stop the pointer from pointing to their object and get my own to modify?
EDIT:
This will work for 2d arrays.
var one = [[1,2],[2,3],[3,4],[4,5]]
var two = one.slice(0)
for(var x = 0; x <= one.length - 1; x++)
{
two[x] = one[x].slice(0)
}
one[1][1] = 10
console.log(one)
console.log(two)
returns:
[[1, 2], [2, 10], [3, 4], [4, 5]]
[[1, 2], [2, 3], [3, 4], [4, 5]]