我有以下代码:
var that = this, k,
colors = ['#468966','#FFF0A5','#FFB033B','#B64926','#8E2800'];
data = [];
that.k=0;
for (var i in answers){
data.push({
key: i,
color: colors[k%5],
values: []
});
answers[i].forEach(function(answer,index){
that.data[k].values.push({
label: 'user_'+index,
value: answer.answer
});
});
that.k++;
}
我希望能够在循环内部访问data[]
和。如何将外部上下文传递给此函数?或者还有其他选择吗?colors
k
for (var i in answers)
我可能省略了一些信息:这是在函数内部,所以代码如下所示:
var createChartData = function(answers){
var that = this, k,
colors = ['#468966','#FFF0A5','#FFB033B','#B64926','#8E2800'];
data = [];
k=0;
for (var i in answers){
data.push({
key: i,
color: colors[k%5],
values: []
});
debugger;
answers[i].forEach(function(answer,index){
that.data[k].values.push({
label: 'user_'+index,
value: answer.answer
});
});
k++;
}
return data;
}
但我认为这应该无关紧要。在for
循环内我无法访问,k
或者colors
令人惊讶的是我可以访问that
。我很困扰...
好的,我可能还有其他问题,因为现在它可以工作了,我稍后会再次检查以找出我的问题所在。