0
我有一個數據總量的函數,但它重複兩次結果。任何人都可以幫助我?重複求和函數兩次
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");
}
數據
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");
這是完全顯示creditType和總。但是當console.log(total)總數重複兩次。我需要使用總積分來顯示圖表。
我沒有看到任何用法或調用'$ scope.totalCreadit()'函數。請爲此提供一個jsfiddle。也可能在這裏標記'css'和'wordpress'。 –
http://jsfiddle.net/33zydfzd/這是一個工作jsfiddle –