Reputation: 263
I have the following data inside my script.js:
$scope.reports = [{
departuredate: "2015-2-27",
routeline: "PASAY - CAGAYAN",
seatingtypescode: "ABS",
tickettripcode: "3",
tripcodetime: "16:30:00"
}, {
departuredate: "2015-2-27",
routeline: "PASAY - CAGAYAN",
seatingtypescode: "ABS",
tickettripcode: "3",
tripcodetime: "16:30:00"
},{
departuredate: "2015-2-27",
routeline: "PASAY - CAGAYAN",
seatingtypescode: "ABS",
tickettripcode: "3",
tripcodetime: "16:30:00"
},{
departuredate: "2015-3-1",
routeline: "Cavite-Laguna",
seatingtypescode: "ABS",
tickettripcode: "4",
tripcodetime: "16:30:00"
}, {
departuredate: "2015-3-1",
routeline: "Cavite-Laguna",
seatingtypescode: "ABS",
tickettripcode: "4",
tripcodetime: "16:30:00"
},{
departuredate: "2015-3-2",
routeline: "Earth-Heaven",
seatingtypescode: "ABS",
tickettripcode: "5",
tripcodetime: "16:30:00"
}];
I want to achieve an output which looks like this:
{
3:{
2015-2-27: 3,
2015-2-28: 0,
2015-3-1: 0,
2015-3-2: 0,
2015-3-3: 0,
routeline: "PASAY - CAGAYAN"
},
4:{
2015-2-27: 0,
2015-2-28: 0,
2015-3-1: 1,
2015-3-2: 0,
2015-3-3: 0,
routeline: "Cavite-Laguna"
}
5:{
2015-2-27: 0,
2015-2-28: 0,
2015-3-1: 0,
2015-3-2: 1,
2015-3-3: 0,
routeline: "Earth-Heaven"
}
}
The 3
, 4
, 5
means tickettripcode
at $scope.reports
. The dates is the range of date entered by the user. I want to do is to count the number of departuredate in certain tickettripcode.
For example, In tickettripcode:3
there are 3 same dates(2015-2-27).In tickettripcode:4
there are 2 same dates(2013-3-1).In tickettripcode:4
there is only one date(2013-3-2). If there is no departuredate in that given date the value should return 0
as you can see above.
I have done a code but I get the wrong output because it prints all 0
. Just like this:
{
3:{
2015-2-27: 0,
2015-2-28: 0,
2015-3-1: 0,
2015-3-2: 0,
2015-3-3: 0,
routeline: "PASAY - CAGAYAN"
},
}
What is the wrong I did. Any help? Thanks very much.
My plunker link is :http://plnkr.co/edit/excSfosrSHUFqF5vDFEO?p=preview
Upvotes: 2
Views: 57
Reputation: 77482
Try this
var details = {};
angular.forEach($scope.reports, function (report) {
if (!details[report.tickettripcode]) {
details[report.tickettripcode] = {};
details[report.tickettripcode].routeline = report.routeline;
}
angular.forEach(pushDatesInside, function (el) {
if (typeof details[report.tickettripcode][el.date] === 'undefined') {
details[report.tickettripcode][el.date] = 0;
}
});
details[report.tickettripcode][report.departuredate] += 1;
});
Upvotes: 1
Reputation: 51
You reset your map[pushDatesInsideValue]
with 0 at the end. Check this link (forked from yours):
http://plnkr.co/edit/VESbDZNvaSR9JqXLbX8b?p=preview
Upvotes: 1