Hi,
This is driving me crazy. I have this code in node.js on the server side:
var player1 = {"id":2,"aids":[{"id":"revive","value":1}]};
var player2 = {"id":32,"aids":[{"id":"revive","value":1}]};
var inturn = {"id":2,"aids":[{"id":"revive","value":1}]};
var outturn = {"id":32,"aids":[{"id":"revive","value":1}]};
outturn.aids.map(a=> { if(a.id == 'revive') a.value = 0 });
console.log(JSON.stringify(inturn.aids)+' '+JSON.stringify(outturn.aids));
it is supposed to produce this:
var player1 = {"id":2,"aids":[{"id":"revive","value":1}]};
var player2 = {"id":32,"aids":[{"id":"revive","value":1}]};
var inturn = {"id":2,"aids":[{"id":"revive","value":1}]};
var outturn = {"id":32,"aids":[{"id":"revive","value":0}]};
I cant reproduce it anywhere else but the problem here is that I want only to modify what is inside outturn.aids and nothing else but this will also modify all other variables with a similar structure. So this is what I get
var player1 = {"id":2,"aids":[{"id":"revive","value":0}]};
var player2 = {"id":32,"aids":[{"id":"revive","value":0}]};
var inturn = {"id":2,"aids":[{"id":"revive","value":0}]};
var outturn = {"id":32,"aids":[{"id":"revive","value":0}]};
however this wont happen if there is a different object preceding the one that is being changed, like this:
var player1 = {"id":2,"aids":[{"id":"otherthing","value":1},{"id":"revive","value":1}]};
var player2 = {"id":32,"aids":[{"id":"otherthing","value":1},{"id":"revive","value":1}]};
var inturn = {"id":2,"aids":[{"id":"otherthing","value":1},{"id":"revive","value":1}]};
var outturn = {"id":32,"aids":[{"id":"revive","value":0}]};
Why is this happening? Since I cant reproduce it somewhere else you might thing is a context thing but it doesnt seem to be the case because console.log shows the change right after outturn.aids.map(a=> { if(a.id == 'revive') a.value = 0 });
which means this is the culprit but why is this line of code modifying all the other variables as well? Makes no sense to me…
Thank you.