To get the total count of articles in a JSON array, I use the following method:
Home.html
<div ng-controller="pfcArticleCountCtrl">Number of Articles {{articlecount.length}} items</div>
Controllers.js
// Calculate total number of articles
pfcControllers.controller('pfcArticleCountCtrl', ['$scope', 'pfcArticles', function ($scope, pfcArticles) {
$scope.articlecount = pfcArticles.query();
}]);
Services.js
// Retrieve articles by ID
pfcServices.factory('pfcArticles', ['$resource', function ($resource) {
return $resource('https://myrestcall.net/tables/articles/:articleID', { articleID: '@id' },
{
'update': { method:'PATCH'}
}
);
}]);
Now, in addition to displaying the overall count, I also want to show the number of articles by category. Here is an example data set:
[
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D4",
"articletitle": "article1",
"articlecategoryid": 1,
"articlesummary": "article 1 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D5",
"articletitle": "article2",
"articlecategoryid": 2,
"articlesummary": "article 2 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D6",
"articletitle": "article3",
"articlecategoryid": 3,
"articlesummary": "article 3 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D7",
"articletitle": "article4",
"articlecategoryid": 1,
"articlesummary": "article 3 summary. "
},
]
In this scenario, the total count is 4, with 2 articles in Category 1. To display this information on a page, it should look like this:
Category 1 (2) Category 2 (1) Category 3 (1)
Total Articles (4)
How can I count the number of articles per category?