Search code examples
javascriptjsonpie-chart

Uncaught SyntaxError: Unexpected token for while adding data to pieChart dynamically


i have imbrecated json objects, i am struggling with this error when i try to loop inside PieData to fill pieChart this is my code

  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"]
  },  }        
       ];

when i if i try to change to static for => for (var r = 0; r < 2; r++) { i always get the same error, any help is appreciatred


Solution

  • You can't use for in array declaration.

    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 will looks cleaner here

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