I have a big multidimensional array that holds references to objects and I need to be able to move a reference from one spot in the array to another and then delete the original reference (or set it to undefined). The problem with this is it then deletes the recent reference.
var data = [[new Obj, new Obj], [new Obj, new Obj]];
function move(fromx, fromy, tox, toy) {
data[tox][toy] = data[fromx][fromy];
delete data[fromx][fromy];
}
Edit: I mean both are gone. data[tox][toy] === undefined; Both references are destroyed, not just data[fromx][fromy]
Yeah, that’s just the
deleteoperator doing what it is supposed to do, which is delete the object which is referred to at [fromx,fromy]. Try simply settingdata[fromx][fromy]tonullor an undefined variable such asallYourBaseBelongToUs(At least I’d hope it’s undefined) or if you’re boringundefinedis also undefined.Sources: