简体   繁体   中英

How to get sum of array nested in object?

{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
    }
]
}};

For each category, I'm trying to count the answered trivia question. How do I get the sum of answered questions for each category (indicated when there is a value present)? I think there's something gone wrong in my loop. I've also tried this with array prototypes. Sorry for the noob question.

$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)
    }
}
});

something like this? (look at the console)

console output:

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() { for(var i = 0; i < service.categories.length; i++ ){ var questionCount = 0; for(var j = 0; j < service.categories[i].questions.length; j++ ) { if (service.categories[i].questions[j].value != '' && service.categories[i].questions[j].value != undefined) { 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> 

I am not used to with angular js but this is how i will do with js. i hope this helps you.



    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

Use map() method provided by Array

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}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM