0

Given an array like this, how would I get a count of all charts in a particular category. Each category can have multiple or no groups.

{
   "categories":[
      {
         "title":"category 1",
         "id":"cat1",
         "groups":[
            {
               "title":"group 1",
               "id":"grp1",
               "charts":[
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  }
               ]
            }
         ]
      },
      {
         "title":"category 2",
         "id":"cat2",
         "charts":[
            {
               "title":"chart 2",
               "id":"chart2",
               "type":"line"
            }
         ]
      },
      {
         "title":"category 3",
         "id":"cat3",
         "charts":[
            {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            }
         ]
      }
   ]
}
1
  • Just loop the elements in the categories array and count them. What have you tried so far? Commented Nov 10, 2016 at 10:09

4 Answers 4

1

Is a one-liner okay?

Assuming data is your JSON structure:

data.categories
    .map(c => [
        c.title,
        c.groups ?
            c.groups.map(g => g.charts.length).reduce((a, b) => a+b) :
            c.charts.length
    ])
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but need to support ie9. elegant solution though
It should still work if you replace the arrow functions with old style functions
1

var object = {
  "categories": [{
    "title": "category 1",
    "id": "cat1",
    "groups": [{
      "title": "group 1",
      "id": "grp1",
      "charts": [{
        "title": "chart 1",
        "id": "chart1",
        "type": "line"
      }]
    }]
  }, {
    "title": "category 2",
    "id": "cat2",
    "charts": [{
      "title": "chart 2",
      "id": "chart2",
      "type": "line"
    }]
  }, {
    "title": "category 3",
    "id": "cat3",
    "charts": [{
      "title": "chart 3",
      "id": "chart3",
      "type": "line"
    }]
  }]
}
var groupPerCategories = [];

object.categories.forEach(function(category) {
  var tot = 0;
  if (category.groups != undefined) {
    category.groups.forEach(function(group) {
      if(group.charts != undefined){
          tot += group.charts.length;
      }
    });
  }
  if (category.charts != undefined) {
    tot += category.charts.length;
  }
  console.log(tot);
});

Comments

0

You could count the properties with default.

var data = { "categories": [{ "title": "category 1", "id": "cat1", "groups": [{ "title": "group 1", "id": "grp1", "charts": [{ "title": "chart 1", "id": "chart1", "type": "line" }] }] }, { "title": "category 2", "id": "cat2", "charts": [{ "title": "chart 2", "id": "chart2", "type": "line" }] }, { "title": "category 3", "id": "cat3", "charts": [{ "title": "chart 3", "id": "chart3", "type": "line" }] }] },
    count = {};

data.categories.forEach(function (a) {
    var countCharts = function (r, a) {
        return r + (a.charts || []).length;
    };
    count[a.title] = (count[a.title] || 0) + 
        (a.groups ||[]).reduce(countCharts, 0) +
        countCharts(0, a);
});

console.log(count);

Comments

0

Hope this will help you :)

var categories = [
      {
         "title":"category 1",
         "id":"cat1",
         "groups":[
            {
               "title":"group 1",
               "id":"grp1",
               "charts":[
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  }
               ]
            }
         ]
      },
      {
         "title":"category 2",
         "id":"cat2",
         "charts":[
            {
               "title":"chart 2",
               "id":"chart2",
               "type":"line"
            }
         ]
      },
      {
         "title":"category 3",
         "id":"cat3",
         "charts":[
            {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            },
           {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            },
           {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            }
         ]
      },
    {
         "title":"category 4",
         "id":"cat4",
         "groups":[
            {
               "title":"group 4",
               "id":"grp4",
               "charts":[
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  },
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  }
               ]
            }
         ]
      }
   ];


function countCategoryItems(data, category) {
   if(!data ) return 0
   if(!category) return 0

   var cat = _.filter(data, function(obj){ return obj.title == category; });
   if(!cat.length) return 0

   var groups = cat[0].groups || []
   if(!groups.length) return cat[0].charts.length
   
   var count = 0
   for (var i=0;i<groups.length;i++) {
        count += groups[i].charts.length
   }
  
   return count
}

$(function() {
console.log(countCategoryItems(categories, 'category 1'))
console.log(countCategoryItems(categories, 'category 2'))
console.log(countCategoryItems(categories, 'category 3'))
console.log(countCategoryItems(categories, 'category 4'))
})
<script src="http://underscorejs.org/underscore.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.