0
我想创建一个可以像status[a][b]
这样的字典,其中a
和b
是在初始化时不知道的随机字符串。错误:状态[x]未定义
这里的确切使用情况:
status = {}
name = {};
time = {};
score = {};
for (i=0; i<$scope.submissions.length; i++)
{
e = $scope.submissions[i];
status[e.rno] = status[e.rno] || {};
time[e.rno] = time[e.rno] || 0;
score[e.rno] = score[e.rno] || 0;
status[e.rno][e.problem] = status[e.rno][e.problem] || 0;
if (e.score == 100 && status[e.rno][e.problem] == 0)
{
status[e.rno][e.problem] = 100;
time[e.rno] += e.id;
score[e.rno] += 100;
}
}
console.log(score["20161230"]);
它引发错误:Error: status[e.rno] is undefined
我永远不会发现。谢谢! – anukul
@anukul只要保持总是使用局部变量的习惯,除非你确实需要一个全局变量。 – Barmar