Here's another way to do it. I can't speak to performance against PSL's method, but I think this reads a little easier to my not-very good at javascript eyes.
<!-- language: lang-js -->
function groupByName(names) {
inputArray = names.split('\n');
outputArray = {};
for (i = 0; i < inputArray.length; i++) {
var keyValuePair = inputArray[i].split(',');
var key = keyValuePair[0];
var count = Number(keyValuePair[1]);
// create element if it doesnt' exist
if (!outputArray[key]) outputArray[key] = 0;
// increment by value
outputArray[key] += count;
}
return outputArray;
}
This will produce an object that looks like this:
<!-- language: lang-js -->
{
"John": 6,
"Jane": 8
}
You can display the name of each property and it's value with ng-repeat:
<!-- language: lang-html -->
<li ng-repeat="(key, value) in groupedNames">
The total for {{key}} is {{value}}
</li>
It's not an overwhelmingly complex bit of javascript, depending on the number of name value pairs. So you can probably even get rid of the manual calc button and just put a $watch
on values
in order to automatically recalculate totals with every change.
<!-- language: lang-js -->
$scope.$watch('values', function() {
$scope.groupedNames = groupByName($scope.values);
});