json - Angularjs: How to print data from object only once if repeated -
my goal print object using ng-repeat
knowing keys repeated. want printed once if case , want calculate average of similar values.
this 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 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 output this:
the output should 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", } ];
you can try angular filter (https://github.com/a8m/angular-filter)
here snippet:
var app = angular.module("myapp", ['angular.filter']); app.controller("myctrl", function($scope) { $scope.getavg = function(values) { var sum = 0; (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>
Comments
Post a Comment