I have a function for total in data but it is repeating the result twice. anyone can help me?
function groupBy(arr, key) {
var newArr = []
, types = {}
, newItem, i, j, cur;
for (i = 0, j = arr.length; i < j; i++) {
cur = arr[i];
if (!(cur[key] in types)) {
types[cur[key]] = {
type: cur[key]
, data: []
};
newArr.push(types[cur[key]]);
}
types[cur[key]].data.push(cur);
}
return newArr;
};
Array.prototype.sum = function (prop) {
var total = 0
for (var i = 0, len = this.length; i < len; i++) {
total += parseInt(this[i][prop])
}
console.log(total);
return total;
}
$scope.totalCreadit = function (st) {
return st.sum("credits");
}
data
var users = [
{
"creditType": "Transfer"
,"credits": "50"
}, {
"creditType": "Transfer"
,"credits": "50"
}
, {
"creditType": "Bought"
,"credits": "50"
}
, {
"creditType": "Spent"
,"credits": "50"
}
, {
"creditType": "Award"
,"credits": "50"
}
, {
"creditType": "Received"
,"credits": "50"
}];
$scope.items = groupBy(users, "creditType");
It is perfectly displaying the creditType and the total. but when console.log(total) the total is repeating twice. I need to use the total of credits to display chart.
Source: AngularJS
from Angular Questions https://angularquestions.com/2017/10/19/repeating-twice-in-sum-function/
via @lzomedia #developer #freelance #web #lzomedia.com
No comments:
Post a Comment