2015-09-04 97 views
0
{service: { 
    categories: [ 
     { 
      name: "category1", 
      id: 1, 
      questions: [ 
       { 
        id: 1, 
        question: "example trivia question here", 
        value: 2 
       }, { 
        id: 2, 
        question: "example trivia question here", 
        value: 3 
       }] 
     }, 
    { 
     name: "category2", 
     id: 2, 
     questions: [ 
      { 
       id: 3, 
       question: "example trivia question here", 
       value: 5 
      } 
     ] 
    }, 
    { 
     name: "category3", 
     id: 3 
    } 
] 
}}; 

对于每个类别,我试图计算回答的琐事问题。我如何获得每个类别的答案总数(当存在价值时表示)?我认为我的循环中出现了问题。我也尝试过使用数组原型。对于noob问题抱歉。如何获取嵌套在对象中的数组的总和?

$scope.questionCount = 0; 

angular.forEach($scope.service, function(categories, questions) { 
    for (var i = 0; i < questions.length; i++) { 
    if (questions[i].value !== null){ 
     triviaCount = 0 
     triviaCount += 1 
     $scope.questionCount.push(triviaCount) 
    } 
} 
}); 
+3

您在for循环的每个迭代复位'triviaCount'为零。 –

+0

我觉得你想要像'angular.forEach($ scope.service.categories,function(category){...})'在顶层?然后在里面,像你一样的另一个循环已经经历了这个类别的问题。实际上,在当前代码中没有任何地方访问questions属性 - 只需创建一个名为“questions”的变量,该变量充满了forEach中每个迭代对象的键。 –

回答

0

使用map()方法

var service = { 
    categories: [ 
     { 
      name: "category1", 
      id: 1, 
      questions: [ 
       { 
        id: 1, 
        question: "example trivia question here", 
        value: 2 
       }, { 
        id: 2, 
        question: "example trivia question here", 
        value: 3 
       }] 
     }, 
    { 
     name: "category2", 
     id: 2, 
     questions: [ 
      { 
       id: 3, 
       question: "example trivia question here", 
       value: 5 
      } 
     ] 
    }, 
    { 
     name: "category3", 
     id: 3 
    } 
] 
}; 


var result = service.categories.map(function(cat) { 
    return { 
     'name' : cat.name, 
     'count' : !!cat.questions ? cat.questions.length : 0 
    } 
}); 

console.log(result); 
// [{ name="category1", count=2}, { name="category2", count=1}, { name="category3", count=0}] 
0

这样的事情? (看控制台)

控制台输出:

category1 # of quesitons: 2 
category2 # of quesitons: 1 
category3 # of quesitons: 0 

var service = { 
 
    categories: [ 
 
     { 
 
      name: "category1", 
 
      id: 1, 
 
      questions: [ 
 
       { 
 
        id: 1, 
 
        question: "example trivia question here", 
 
        value: 2 
 
       }, { 
 
        id: 2, 
 
        question: "example trivia question here", 
 
        value: 3 
 
       }, 
 
       { 
 
        id: 9, 
 
        question: "example trivia question here", 
 
        value: '', 
 
       } 
 
      ] 
 
     }, 
 
     { 
 
      name: "category2", 
 
      id: 2, 
 
      questions: [ 
 
       { 
 
        id: 3, 
 
        question: "example trivia question here", 
 
        value: 5 
 
       }, 
 
       { 
 
        id: 7, 
 
        question: "example trivia question here", 
 
        value: '', 
 
       } 
 
      ] 
 
     }, 
 
     { 
 
      name: "category3", 
 
      id: 3, 
 
      questions: [], 
 
     } 
 
    ] 
 
}; 
 
$(document).ready(function() { 
 
\t for(var i = 0; i < service.categories.length; i++){ 
 
     var questionCount = 0; 
 
    \t for(var j = 0; j < service.categories[i].questions.length; j++) { 
 
     \t if (service.categories[i].questions[j].value != '' && service.categories[i].questions[j].value != undefined) { 
 
      \t questionCount++; 
 
      }    
 
     } 
 
     console.log(service.categories[i].name, ' # of quesitons: ', questionCount); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

在你的代码中,你可以做'questionCount = service.categories [i] .questions.length'没有循环 – Grundy

+0

好吧。我正在跟进。但是,这似乎可以计算每个问题。我只是试图计算具有“价值”的问题。 – NewGuy504

+0

@ NewGuy504好的,你也试图计算有价值的问题,2个问题有价值,所以输出为2,或者你是否想要计算如下问题的价值:2个问题的值为5和3,所以输出8? – indubitablee

0

我不习惯与角的js但是这是我将如何与JS做。我希望这可以帮助你。通过阵列提供

 


    var json = { 
     service: { 
      categories: [{ 
       name: "category1", 
       id: 1, 
       questions: [{ 
        id: 1, 
        question: "example trivia question here", 
        value: 2 
       }, { 
        id: 2, 
        question: "example trivia question here", 
        value: 3 
       }] 
      }, { 
       name: "category2", 
       id: 2, 
       questions: [{ 
        id: 3, 
        question: "example trivia question here", 
        value: 5 
       }] 
      }, { 
       name: "category3", 
       id: 3 
      }] 
     } 
    }; 

    var qCount = 0; 

    categories = json.service.categories; 
    for (var category in categories) { 
     var temp = categories[category]; 
     if (temp.questions !== undefined) { 
      var questions = temp.questions; 
      for (var que in questions) { 
       if (questions[que].value !== undefined) 
        qCount++; 
      } 
     } 
    } 

    console.log(qCount); //here is your question count