AngularJS Array - How to add amount if same category [duplicate]

Clash Royale CLAN TAG#URR8PPP
AngularJS Array - How to add amount if same category [duplicate]
This question already has an answer here:
How to add the amount if they have same category in angular and remove the duplicate entry.
For example:
self.lineDetails = [id:'0',category:'AAA',amount:'500',
id:'1',category:'BBB',amount:'200',
id:'2',category:'AAA',amount:'300']
Desired Output:
self.newLineDetails = [id:'0',category:'AAA',amount:'800',
id:'1',category:'BBB',amount:'200']
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@PM. I don't want to use a third party library. I was hoping the angularjs would be enough.
– klent
Aug 10 at 4:46
You don't need AngularJS specifically to solve this problem. It can be done using plain JS.
– 31piy
Aug 10 at 4:54
2 Answers
2
You can use the angularjs filter groupBy
DEMO
var myApp = angular.module('myApp', ['angular.filter']);
myApp.controller('MyController', function($scope)
$scope.data = [id:'0',category:'AAA',amount:'500',
id:'1',category:'BBB',amount:'200',
id:'2',category:'AAA',amount:'300'];
$scope.getAmountSum = function(items)
return items
.map(function(x) return x.amount; )
.reduce(function(a, b) return parseInt(a) + parseInt(b); );
;
);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.js"></script>
<div ng-app="myApp" ng-controller="MyController">
<ul>
<li ng-repeat="(key, items) in data | groupBy: 'category'">
category: items[0].category, amount: getAmountSum(items)
</li>
</ul>
</div>
I get the following error: TypeError: items.map is not a function at Object.self.getAmountSum
– klent
Aug 10 at 8:41
look at the demo above
– Sajeetharan
Aug 10 at 8:42
You can try creating a map sort of json in angular js as :
self.lineDetails = [id:'0',category:'AAA',amount:'500',
id:'1',category:'BBB',amount:'200',
id:'2',category:'AAA',amount:'300'];
self.lineDetailsMap = ;
angular.forEach(self.lineDetails, function(lineDetail)
if(self.lineDetailsMap[lineDetail.category] !== undefined)
var lineDetailOld = self.lineDetailsMap[lineDetail.category] ;
lineDetail.amount = parseInt(lineDetailOld.amount) + parseInt(lineDetail.amount);
self.lineDetailsMap[lineDetail.category] = lineDetail;
);
Now you can iterate upon this map to fetch the new list:
self.newLineDetails = ;
angular.forEach(self.lineDetailsMap,function(value,key)
self.newLineDetails.push(value);
);
Let me know if you get any issues
self.newLineDetails is coming up blank.
– klent
Aug 10 at 7:03
Can you please check what is getting populated in self.lineDetailsMap
– codeLover
Aug 10 at 7:13
it generates an array but the amount is wrong.
– klent
Aug 10 at 7:41
I tried using lineDetailsMap instead but the amount is still wrong.
– klent
Aug 10 at 8:36
Can you show what is getting stored in map
– codeLover
Aug 10 at 8:46
You want to group the data, for that you can use the third party libraries, refer stackoverflow.com/questions/35405937/…
– PM.
Aug 10 at 4:42