我有两个对象:其中一个包含播放器列表,另一个包含加载屏幕中的播放器。
当我从playersNotReady对象中移除一个玩家时,它也会从玩家列表中删除该玩家,我不想这样做。
var players = {
1: "Player1",
2: "Player2"
};
var playersNotReady = players;
delete playersNotReady[1];
console.log(players); // {2: "Player2"}
console.log(playersNotReady); // {2: "Player2"}

为什么会发生这种情况?如何从playerNotReady对象中移除玩家而不将其从实际玩家列表中删除?
答案 0 :(得分:1)
您没有创建新变量。你只是通过参考指出。您可以使用def export_to_xml(request):
if request.method == 'POST':
#gives list of id of inputs
list_of_input_ids=request.POST.getlist('agreeCheckbox')
from django.core import serializers
data = serializers.serialize("xml", Catalog.objects.filter(id__in=list_of_input_ids))
from django.core.files import File
f = open('catalogs.xml', 'w')
myfile = File(f)
myfile.write(data)
myfile.close()
return HttpResponse("All done!")
来克隆/创建新对象。
Object.assign
Doc:Object.assign
另一个选项是使用var players = {
1: "Player1",
2: "Player2"
};
var playersNotReady = Object.assign({}, players);
delete playersNotReady[1];
console.log(players);
console.log(playersNotReady);
,如:
Destructuring Assignment
Doc:Spread
答案 1 :(得分:0)
执行CDI
时,您没有创建新对象,只是对 private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Abort;
}
对象的新引用,这是因为javascript中的对象为passed by reference。
要创建新对象,可以使用ES6扩展运算符:var playersNotReady = players;
答案 2 :(得分:0)
当您指定var playersNotReady = players;
时,它实际上会为玩家创建参考。不是一个新的对象。您可以按Object.assign
复制对象。
var players = {
1: "Player1",
2: "Player2"
};
var playersNotReady = Object.assign({}, players);
delete playersNotReady[1];
console.log(players); // {2: "Player2"}
console.log(playersNotReady); // {2: "Player2"}