2015-09-04 95 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 

 
相關問題