Uncaught SyntaxError:动态添加数据到pieChart时出现意外令牌

时间:2018-07-30 15:49:14

标签: javascript json pie-chart

我有混杂的json对象,当我尝试在PieData内循环填充pieChart时,我正在为这个错误而苦苦挣扎

  var PieData        = [

       for(b in quizs[i].quests[j].reps){
           //quizs[i].quests[j]["quizId"]
        /*   if(quizs[i].quests[j].reps[b]["stat"]==null){
               var l = 1;
           }
           else{
               var l =quizs[i].quests[j].reps[b]["stat"]
           }*/

           {
          {%set h = 'hex'%}        


    value    : 2,
    startAngle: 240,
    color    : '{{h}}',  
    label    : quizs[i].quests[j].reps[b]["rep"]
  },  }        
       ];

当我尝试将=>更改为static时(for r = 0; r <2; r ++){ 我总是遇到相同的错误,感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您不能在数组声明中使用for

var PieData = [];
for (var b in quizs[i].quests[j].reps) {
    PieData.push({
        value: 2,
        startAngle: 240,
        color: "{{h}}",
        label: quizs[i].quests[j].reps[b]["rep"]
    });
}

Array#map在这里看起来更干净

var PieData = quizs[i].quests[j].reps.map(i => ({
    value: 2,
    startAngle: 240,
    color: "{{h}}",
    label: i.rep
}));