0

I have this code:

 $scope.showReport = function(datas){
    data.post('dailyReservationReport',{date:$scope.date}).then(function (response){
        $scope.reports = response.data;
        console.log($scope.reports);

        var sum =0;
        for(var index in $scope.reports){
            if($scope.reports[index].departuredate == "2015-03-11"){
              sum += $scope.reports[index].departuredate;
            }
        }
        console.log(sum);

    })
};

The problem I was have is I want to get the sum of data with departure date which is equal to 2015-03-11. I have 2 data so the sum should output 2.

But this is the results in my console.log:

02015-03-112015-03-11

What I did wrong? Please help

1
  • you are adding date here not data. try something like this: sum += $scope.reports[index].data or whatever key it has instead of data Commented Mar 11, 2015 at 7:31

3 Answers 3

2

As per the line in your question:: "I have 2 data so the sum should output 2"., i think you just need to increment sum, as:

var sum =0;
for(var index in $scope.reports){
    if($scope.reports[index].departuredate == "2015-03-11"){
        sum++;
    }
}
console.log(sum);
Sign up to request clarification or add additional context in comments.

Comments

0

We have to use parseInt() because our value stored in var data type it will be in string format, if we are doing addition for this then it will return Concatenation of string , So we have to use parseInt().

var sum =0;
    for(var index in $scope.reports){
        if($scope.reports[index].departuredate == "2015-03-11"){
          sum = parseInt(sum)+parseInt($scope.reports[index].departuredate); -->changed this line
        }
    }

Comments

0

You could also count items with a certain departure date using a filter:

$filter('filter')($scope.reports, { 'departuredate': '2015-03-11' }).length

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.