TSR
TSR

Reputation: 20456

Angularjs: How to print data from object only once if repeated

My goal is to print an object using ng-repeat but knowing that some keys are repeated. I want it to be printed once only if this case and I want to calculate the average of its similar values.

This is my data.

[  
   {  
      "0":"1",
      "1":"Creativity",
      "2":"5",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"1",
      "Title":"Creativity",
      "Value":"5",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"2",
      "1":"Courage",
      "2":"4",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"2",
      "Title":"Courage",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"3",
      "1":"Honnesty",
      "2":"4",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"3",
      "Title":"Honnesty",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"4",
      "1":"Ambition",
      "2":"3",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"4",
      "Title":"Ambition",
      "Value":"3",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"5",
      "1":"Integrity",
      "2":"4",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"5",
      "Title":"Integrity",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"165",
      "1":"Creativity",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"165",
      "Title":"Creativity",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"166",
      "1":"Courage",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"166",
      "Title":"Courage",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"167",
      "1":"Honnesty",
      "2":"2",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"167",
      "Title":"Honnesty",
      "Value":"2",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"168",
      "1":"Ambition",
      "2":"3",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"168",
      "Title":"Ambition",
      "Value":"3",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"169",
      "1":"Integrity",
      "2":"2",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"169",
      "Title":"Integrity",
      "Value":"2",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"170",
      "1":"Creativity",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"170",
      "Title":"Creativity",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"171",
      "1":"Courage",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"171",
      "Title":"Courage",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"172",
      "1":"Honnesty",
      "2":"5",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"172",
      "Title":"Honnesty",
      "Value":"5",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"173",
      "1":"Ambition",
      "2":"3",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"173",
      "Title":"Ambition",
      "Value":"3",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"174",
      "1":"Integrity",
      "2":"5",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"174",
      "Title":"Integrity",
      "Value":"5",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   }
]

This is my html

<h3>Overall Ratings: {{ getTotal() }}</h3>
        <div class="row">
            <div class="container">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Label</th>
                            <th>Rate</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="profile in data | filter:searchFilter">
                            <td>{{profile.Title}}</td>
                            <td>{{profile.Value}}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

The problem is that the output is like this:

enter image description here

The output should be like this:

 [{
                "Value": 4.25,
                "Title": "Creativity",
            },
            {
                "Value": 2.7,
                "Title": "Courage",
            },
            {
                "Value": 3.33,
                "Title": "Honnesty",
            }, {
                "Value": 4.5,
                "Title": "Ambition",
            }, {
                "Value": 5,
                "Title": "Integrity",
            }
        ];

Upvotes: 1

Views: 743

Answers (4)

Duong Dang
Duong Dang

Reputation: 349

You can try with the Angular filter (https://github.com/a8m/angular-filter)

Here is snippet:

var app = angular.module("myApp", ['angular.filter']);
app.controller("myCtrl", function($scope) {
    $scope.getAvg = function(values) {
      var sum = 0;
      for (var i=0;i<values.length;i++) {
        sum += parseInt(values[i].Value)
      }
      
      return sum/values.length
    };
  
    $scope.data = [  
       {  
          "0":"1",
          "1":"Creativity",
          "2":"5",
          "3":"1",
          "4":"2017-01-17 21:24:42",
          "5":"ratings",
          "6":null,
          "ID":"1",
          "Title":"Creativity",
          "Value":"5",
          "Parent":"1",
          "Timestamp":"2017-01-17 21:24:42",
          "Category":"ratings",
          "Author":null
       },
       {  
          "0":"2",
          "1":"Courage",
          "2":"4",
          "3":"1",
          "4":"2017-01-17 21:24:42",
          "5":"ratings",
          "6":null,
          "ID":"2",
          "Title":"Courage",
          "Value":"4",
          "Parent":"1",
          "Timestamp":"2017-01-17 21:24:42",
          "Category":"ratings",
          "Author":null
       },
       {  
          "0":"3",
          "1":"Honnesty",
          "2":"4",
          "3":"1",
          "4":"2017-01-17 21:24:42",
          "5":"ratings",
          "6":null,
          "ID":"3",
          "Title":"Honnesty",
          "Value":"4",
          "Parent":"1",
          "Timestamp":"2017-01-17 21:24:42",
          "Category":"ratings",
          "Author":null
       },
       {  
          "0":"4",
          "1":"Ambition",
          "2":"3",
          "3":"1",
          "4":"2017-01-17 21:24:42",
          "5":"ratings",
          "6":null,
          "ID":"4",
          "Title":"Ambition",
          "Value":"3",
          "Parent":"1",
          "Timestamp":"2017-01-17 21:24:42",
          "Category":"ratings",
          "Author":null
       },
       {  
          "0":"5",
          "1":"Integrity",
          "2":"4",
          "3":"1",
          "4":"2017-01-17 21:24:42",
          "5":"ratings",
          "6":null,
          "ID":"5",
          "Title":"Integrity",
          "Value":"4",
          "Parent":"1",
          "Timestamp":"2017-01-17 21:24:42",
          "Category":"ratings",
          "Author":null
       },
       {  
          "0":"165",
          "1":"Creativity",
          "2":"4",
          "3":"1",
          "4":"2017-01-20 21:12:25",
          "5":"ratings",
          "6":"18",
          "ID":"165",
          "Title":"Creativity",
          "Value":"4",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:12:25",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"166",
          "1":"Courage",
          "2":"4",
          "3":"1",
          "4":"2017-01-20 21:12:25",
          "5":"ratings",
          "6":"18",
          "ID":"166",
          "Title":"Courage",
          "Value":"4",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:12:25",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"167",
          "1":"Honnesty",
          "2":"2",
          "3":"1",
          "4":"2017-01-20 21:12:25",
          "5":"ratings",
          "6":"18",
          "ID":"167",
          "Title":"Honnesty",
          "Value":"2",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:12:25",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"168",
          "1":"Ambition",
          "2":"3",
          "3":"1",
          "4":"2017-01-20 21:12:25",
          "5":"ratings",
          "6":"18",
          "ID":"168",
          "Title":"Ambition",
          "Value":"3",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:12:25",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"169",
          "1":"Integrity",
          "2":"2",
          "3":"1",
          "4":"2017-01-20 21:12:25",
          "5":"ratings",
          "6":"18",
          "ID":"169",
          "Title":"Integrity",
          "Value":"2",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:12:25",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"170",
          "1":"Creativity",
          "2":"4",
          "3":"1",
          "4":"2017-01-20 21:16:53",
          "5":"ratings",
          "6":"18",
          "ID":"170",
          "Title":"Creativity",
          "Value":"4",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:16:53",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"171",
          "1":"Courage",
          "2":"4",
          "3":"1",
          "4":"2017-01-20 21:16:53",
          "5":"ratings",
          "6":"18",
          "ID":"171",
          "Title":"Courage",
          "Value":"4",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:16:53",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"172",
          "1":"Honnesty",
          "2":"5",
          "3":"1",
          "4":"2017-01-20 21:16:53",
          "5":"ratings",
          "6":"18",
          "ID":"172",
          "Title":"Honnesty",
          "Value":"5",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:16:53",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"173",
          "1":"Ambition",
          "2":"3",
          "3":"1",
          "4":"2017-01-20 21:16:53",
          "5":"ratings",
          "6":"18",
          "ID":"173",
          "Title":"Ambition",
          "Value":"3",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:16:53",
          "Category":"ratings",
          "Author":"18"
       },
       {  
          "0":"174",
          "1":"Integrity",
          "2":"5",
          "3":"1",
          "4":"2017-01-20 21:16:53",
          "5":"ratings",
          "6":"18",
          "ID":"174",
          "Title":"Integrity",
          "Value":"5",
          "Parent":"1",
          "Timestamp":"2017-01-20 21:16:53",
          "Category":"ratings",
          "Author":"18"
       }
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
    <table ng-repeat="(key, value) in data | groupBy: 'Title'">
      <tr>
        <td>{{key}}</td>
        <td>{{getAvg(value)}}
      </tr>
    </table>
  </div>

Upvotes: 2

jukben
jukben

Reputation: 1098

My ES6 solution :)

Edit: a little bit tidied up. 🛀

const data = [{
  "0": "1",
  "1": "Creativity",
  "2": "5",
  "3": "1",
  "4": "2017-01-17 21:24:42",
  "5": "ratings",
  "6": null,
  "ID": "1",
  "Title": "Creativity",
  "Value": "5",
  "Parent": "1",
  "Timestamp": "2017-01-17 21:24:42",
  "Category": "ratings",
  "Author": null
}, {
  "0": "2",
  "1": "Courage",
  "2": "4",
  "3": "1",
  "4": "2017-01-17 21:24:42",
  "5": "ratings",
  "6": null,
  "ID": "2",
  "Title": "Courage",
  "Value": "4",
  "Parent": "1",
  "Timestamp": "2017-01-17 21:24:42",
  "Category": "ratings",
  "Author": null
}, {
  "0": "3",
  "1": "Honnesty",
  "2": "4",
  "3": "1",
  "4": "2017-01-17 21:24:42",
  "5": "ratings",
  "6": null,
  "ID": "3",
  "Title": "Honnesty",
  "Value": "4",
  "Parent": "1",
  "Timestamp": "2017-01-17 21:24:42",
  "Category": "ratings",
  "Author": null
}, {
  "0": "4",
  "1": "Ambition",
  "2": "3",
  "3": "1",
  "4": "2017-01-17 21:24:42",
  "5": "ratings",
  "6": null,
  "ID": "4",
  "Title": "Ambition",
  "Value": "3",
  "Parent": "1",
  "Timestamp": "2017-01-17 21:24:42",
  "Category": "ratings",
  "Author": null
}, {
  "0": "5",
  "1": "Integrity",
  "2": "4",
  "3": "1",
  "4": "2017-01-17 21:24:42",
  "5": "ratings",
  "6": null,
  "ID": "5",
  "Title": "Integrity",
  "Value": "4",
  "Parent": "1",
  "Timestamp": "2017-01-17 21:24:42",
  "Category": "ratings",
  "Author": null
}, {
  "0": "165",
  "1": "Creativity",
  "2": "4",
  "3": "1",
  "4": "2017-01-20 21:12:25",
  "5": "ratings",
  "6": "18",
  "ID": "165",
  "Title": "Creativity",
  "Value": "4",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:12:25",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "166",
  "1": "Courage",
  "2": "4",
  "3": "1",
  "4": "2017-01-20 21:12:25",
  "5": "ratings",
  "6": "18",
  "ID": "166",
  "Title": "Courage",
  "Value": "4",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:12:25",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "167",
  "1": "Honnesty",
  "2": "2",
  "3": "1",
  "4": "2017-01-20 21:12:25",
  "5": "ratings",
  "6": "18",
  "ID": "167",
  "Title": "Honnesty",
  "Value": "2",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:12:25",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "168",
  "1": "Ambition",
  "2": "3",
  "3": "1",
  "4": "2017-01-20 21:12:25",
  "5": "ratings",
  "6": "18",
  "ID": "168",
  "Title": "Ambition",
  "Value": "3",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:12:25",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "169",
  "1": "Integrity",
  "2": "2",
  "3": "1",
  "4": "2017-01-20 21:12:25",
  "5": "ratings",
  "6": "18",
  "ID": "169",
  "Title": "Integrity",
  "Value": "2",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:12:25",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "170",
  "1": "Creativity",
  "2": "4",
  "3": "1",
  "4": "2017-01-20 21:16:53",
  "5": "ratings",
  "6": "18",
  "ID": "170",
  "Title": "Creativity",
  "Value": "4",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:16:53",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "171",
  "1": "Courage",
  "2": "4",
  "3": "1",
  "4": "2017-01-20 21:16:53",
  "5": "ratings",
  "6": "18",
  "ID": "171",
  "Title": "Courage",
  "Value": "4",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:16:53",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "172",
  "1": "Honnesty",
  "2": "5",
  "3": "1",
  "4": "2017-01-20 21:16:53",
  "5": "ratings",
  "6": "18",
  "ID": "172",
  "Title": "Honnesty",
  "Value": "5",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:16:53",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "173",
  "1": "Ambition",
  "2": "3",
  "3": "1",
  "4": "2017-01-20 21:16:53",
  "5": "ratings",
  "6": "18",
  "ID": "173",
  "Title": "Ambition",
  "Value": "3",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:16:53",
  "Category": "ratings",
  "Author": "18"
}, {
  "0": "174",
  "1": "Integrity",
  "2": "5",
  "3": "1",
  "4": "2017-01-20 21:16:53",
  "5": "ratings",
  "6": "18",
  "ID": "174",
  "Title": "Integrity",
  "Value": "5",
  "Parent": "1",
  "Timestamp": "2017-01-20 21:16:53",
  "Category": "ratings",
  "Author": "18"
}]

const modifiedData = Object.values(
  [...new Set(data.map(({Title}) => Title))]
  .reduce((a, v, i) => ({
    ...a, [i]: {
      "Title": v,
      "Value": data
        .filter(({Title}) => Title === v)
        .map(({Value}) => +Value)
        .reduce((a, v, i, {length}) => {
          const sum = a + v;
          return (i === length - 1) ? sum / length : sum
        })
    }
  }), {})
)

console.log(modifiedData);

Upvotes: 2

korteee
korteee

Reputation: 2682

You can create a simple service which actually filters the data before rendering them to the DOM. See the plunker

app.service('calcAverage', function () {
    return function (input) {
        output = {
            titles: [],
            values: [],
            repeated:[]
        };
        var tempIndex = 0;
        angular.forEach(input, function (val, key) {
            tempIndex = output.titles.indexOf(val.Title);
            if (tempIndex === -1) {
                output.titles.push(val.Title);
                output.values.push(val.Value);
                output.repeated.push(1);
            } else {
                output.repeated[tempIndex] += 1
                output.values[tempIndex] = ((parseInt(output.values[tempIndex]) + parseInt(val.Value)));
            }
        })

        return output.titles.map(function (val, index) {

            return {
                Title: val,
                Value: (output.values[index] / output.repeated[tempIndex]).toFixed(2)
            }
        })


    }
});

Then in your controller:

$scope.data = calcAverage($scope.data);

Upvotes: 0

Supradeep
Supradeep

Reputation: 3266

You can loop through the original data, calculate the average of values and repeat them as shown below. I've used lodash to make it easy to work with arrays and objects here.

angular.module('app', [])
 .controller('myCtrl', function ($scope) {
   $scope.data = [  
   {  
      "0":"1",
      "1":"Creativity",
      "2":"5",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"1",
      "Title":"Creativity",
      "Value":"5",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"2",
      "1":"Courage",
      "2":"4",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"2",
      "Title":"Courage",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"3",
      "1":"Honnesty",
      "2":"4",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"3",
      "Title":"Honnesty",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"4",
      "1":"Ambition",
      "2":"3",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"4",
      "Title":"Ambition",
      "Value":"3",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"5",
      "1":"Integrity",
      "2":"4",
      "3":"1",
      "4":"2017-01-17 21:24:42",
      "5":"ratings",
      "6":null,
      "ID":"5",
      "Title":"Integrity",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-17 21:24:42",
      "Category":"ratings",
      "Author":null
   },
   {  
      "0":"165",
      "1":"Creativity",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"165",
      "Title":"Creativity",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"166",
      "1":"Courage",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"166",
      "Title":"Courage",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"167",
      "1":"Honnesty",
      "2":"2",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"167",
      "Title":"Honnesty",
      "Value":"2",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"168",
      "1":"Ambition",
      "2":"3",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"168",
      "Title":"Ambition",
      "Value":"3",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"169",
      "1":"Integrity",
      "2":"2",
      "3":"1",
      "4":"2017-01-20 21:12:25",
      "5":"ratings",
      "6":"18",
      "ID":"169",
      "Title":"Integrity",
      "Value":"2",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:12:25",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"170",
      "1":"Creativity",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"170",
      "Title":"Creativity",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"171",
      "1":"Courage",
      "2":"4",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"171",
      "Title":"Courage",
      "Value":"4",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"172",
      "1":"Honnesty",
      "2":"5",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"172",
      "Title":"Honnesty",
      "Value":"5",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"173",
      "1":"Ambition",
      "2":"3",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"173",
      "Title":"Ambition",
      "Value":"3",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   },
   {  
      "0":"174",
      "1":"Integrity",
      "2":"5",
      "3":"1",
      "4":"2017-01-20 21:16:53",
      "5":"ratings",
      "6":"18",
      "ID":"174",
      "Title":"Integrity",
      "Value":"5",
      "Parent":"1",
      "Timestamp":"2017-01-20 21:16:53",
      "Category":"ratings",
      "Author":"18"
   }
];
   $scope.titles = _.uniq(_.map($scope.data, 'Title')); 
  
  $scope.modifiedData = [];
   angular.forEach($scope.titles, function (title){
     var eachCategory = _.filter($scope.data, {'Title': title});
     var sum = 0;
     _.map(eachCategory, function (each){
       sum += parseInt(each.Value);
     });
     if(!$scope.modifiedData[title]){
      $scope.modifiedData.push({'Title': title, 'Value': +(sum/eachCategory.length).toFixed(2)});
       }
   });
});
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="row" ng-app="app" ng-controller="myCtrl">
            <div class="container">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Label</th>
                            <th>Rate</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="profile in modifiedData | filter:searchFilter">
                            <td>{{profile.Title}}</td>
                            <td>{{profile.Value}}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

Upvotes: 0

Related Questions