1

I receive a JSON response like this:

{
    "productDesc": "Other posterior corneal dystrophies",
    "ProductId": 1,
    "productName": "Keylex",
    "productPrice": 3529.24,
    "productStatus": false,
    "productStock": 23,
    "productModifyDate": "2016-10-13T20:13:12",
    "productUrl": "http://dummyimage.com/153x172.jpg/5fa2dd/ffffff",
    "ReviewProducts": [
      {
        "ratingReview": 8.2,
        "reviewDesc": "aaaaa",
        "ReviewProductIdNumber": 1,
        "User": {
          "username": "hsullivan0",
          "UserId": 1,
          "name": "Heather",
          "lastName": "Sullivan"
        }
      },
      {
        "ratingReview": 6.8,
        "reviewDesc": "mattis pulvinar nulla pede ullamcorper augue a suscipit nulla elit ac nulla sed",
        "ReviewProductIdNumber": 2,
        "User": {
          "username": "jcarrod",
          "UserId": 878,
          "name": "Jessica",
          "lastName": "Carr"
        }
      }
    ]
  }

What I want to do is calculate the average ratingReview from each product and show it, right now my html looks like this:

<div class="col-sm-4 col-lg-4 col-md-4" ng-repeat="x in product | limitTo:30">
    <div class="thumbnail">
        <img ng-src="{{x.productUrl}}" alt="">
        <div class="caption">
            <h4 class="pull-right">{{x.productPrice | currency}}</h4>
            <h4>
                <a href="#">{{x.productName}}</a>
            </h4>
            <p>{{x.productDesc}}.</p>
        </div>
        <div class="ratings" ng-repeat="review in x.ReviewProducts">
            <p class="pull-right">{{review.ratingReview}}</p>
            <p>
                <span class="glyphicon glyphicon-star"></span>
            </p>
        </div>
    </div>
</div>

Actually looks like this in the browser: Preview

What I want is something like this: Expected Result

BTW not all products have a review rating so in case is empty show nothing

2
  • 1
    You probably want to calculate the average before you send it to the front end. Your message from the server would come with that in a header, and then the list of products. Commented Apr 5, 2017 at 13:54
  • because you use repeat , it will repeat all review and show like this Commented Apr 5, 2017 at 13:54

3 Answers 3

1

Create function as below

$scope.getAvg= function(ReviewProducts){
    var total = 0;
    for(var i = 0; i < ReviewProducts.length; i++) 
    {
       total += ReviewProducts[i].ratingReview;
    }
    var avg = total / ReviewProducts.length;
    return avg;
}

call this method in div as below

<div>{{getAvg(ReviewProducts)}}</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, can you help me with the stars? Now that I have the average how can I repeat the stars according to the average
Don't forget to check the length (don't divide by 0), see my answer.
Also try to avoid for loops, use angular.forEach instead. So you won't have weird surprises with angular arrays.
0

You can use a function:

    <div class="ratings">
        <p class="pull-right">{{getAverage(x.ReviewProducts}}</p>
        <p>
            <span class="glyphicon glyphicon-star"></span>
        </p>
    </div>

and:

$scope.getAverage = function(reviews){
    var sum = 0;
    var cpt = 0;
    angular.forEach(reviews, function(review){
        cpt++;
        sum += review.ratingReview;
    });
    if(cpt === 0){
        return 0;
    }
    return sum/cpt;
}

Comments

0

You can try with filter like below.

.filter('ratingavg', function() {
    return function(data, key) {
      if (angular.isUndefined(data) || angular.isUndefined(key))
        return 0;
      var sum = 0;    
      angular.forEach(data, function(v, k) {
        sum = sum + parseInt(v[key]);
      });
      return sum / data.length;
    }

 })

<div class="col-sm-4 col-lg-4 col-md-4" ng-repeat="x in product | limitTo:30">
    <div class="thumbnail">
        <img ng-src="{{x.productUrl}}" alt="">
        <div class="caption">
            <h4 class="pull-right">{{x.productPrice | currency}}</h4>
            <h4>
                <a href="#">{{x.productName}}</a>
            </h4>
            <p>{{x.productDesc}}.</p>
        </div>
   <div class="ratings"> {{x.ReviewProducts| ratingavg:'ratingReview'}}</div>          
    </div>
</div>

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.