我有一个与此类似的数组:
var programs_array = [
{"id":3543,"category":"1","target_revenue":1845608},
{"id":2823,"category":"1","target_revenue":1627994},
{"id":1611,"category":"1","target_revenue":1450852},
{"id":1624,"category":"1","target_revenue":25473},
{"id":4626,"category":"2","target_revenue":253048},
{"id":5792,"category":"2","target_revenue":298468},
{"id":5799,"category":"2","target_revenue":256815},
{"id":5171,"category":"2","target_revenue":239090},
{"id":4064,"category":"3","target_revenue":119048},
{"id":2322,"category":"3","target_revenue":59146},
{"id":3466,"category":"3","target_revenue":29362},
{"id":3442,"category":"3","target_revenue":149860},
{"id":1254,"category":"3","target_revenue":15600},
{"id":1685,"category":"3","target_revenue":45463}
];
如果“类别”等于2,我希望所有“ target_revenue”值的总和。目前,我正在这样做,但是我想确保自己以最有效的方式这样做。
Array.prototype.sum_cat = function (prop, cat, val) {
var total = 0
for ( var i = 0, _len = this.length; i < _len; i++ ) {
if(this[i][cat]==val){total += this[i][prop]}
}
return total
}
console.log('total 2: '+programs_array.sum_cat('target_revenue','category',2));
这是一个小提琴:https://jsfiddle.net/26v48djp/
答案 0 :(得分:1)
我将使用reduce
,如果category
为2,则添加到累加器中:
const programs_array=[{"id":3543,"category":"1","target_revenue":1845608},{"id":2823,"category":"1","target_revenue":1627994},{"id":1611,"category":"1","target_revenue":1450852},{"id":1624,"category":"1","target_revenue":25473},{"id":4626,"category":"2","target_revenue":253048},{"id":5792,"category":"2","target_revenue":298468},{"id":5799,"category":"2","target_revenue":256815},{"id":5171,"category":"2","target_revenue":239090},{"id":4064,"category":"3","target_revenue":119048},{"id":2322,"category":"3","target_revenue":59146},{"id":3466,"category":"3","target_revenue":29362},{"id":3442,"category":"3","target_revenue":149860},{"id":1254,"category":"3","target_revenue":15600},{"id":1685,"category":"3","target_revenue":45463}]
const getSum = (findCat) => programs_array.reduce((a, { category, target_revenue }) => (
category === findCat
? a + target_revenue
: a
), 0);
console.log(getSum("2"));
答案 1 :(得分:1)
您可以使用reduce链式过滤器来轻松,简洁地完成此操作。
const sum = programs_array.filter(e => e.category === '2').reduce((acc, element) => acc + element.target_revenue);
或者,如果您想要性能稍高但不太简洁的方法,则可以执行以下操作。但是,这种大小的数组的差异可能可忽略。
const sum = programs_array.reduce((acc, element) => {
return element.category === '2' ? (acc + element.target_revenue) : acc;
});
答案 2 :(得分:0)
您可以使用array.reduce()
let arr = [{"id":3543,"category":"1","target_revenue":1845608}, {"id":2823,"category":"1","target_revenue":1627994}, {"id":1611,"category":"1","target_revenue":1450852}, {"id":1624,"category":"1","target_revenue":25473}, {"id":4626,"category":"2","target_revenue":253048}, {"id":5792,"category":"2","target_revenue":298468}, {"id":5799,"category":"2","target_revenue":256815}, {"id":5171,"category":"2","target_revenue":239090}, {"id":4064,"category":"3","target_revenue":119048}, {"id":2322,"category":"3","target_revenue":59146}, {"id":3466,"category":"3","target_revenue":29362}, {"id":3442,"category":"3","target_revenue":149860}, {"id":1254,"category":"3","target_revenue":15600}, {"id":1685,"category":"3","target_revenue":45463} ];
function getSum(prop, val){
return arr.reduce((a,curr)=> curr.category === val ? a + curr[prop] : a,0);
}
console.log(getSum("target_revenue", "2"));
答案 3 :(得分:0)
通常,for
循环将是最有效的解决方案。
我将在稍作修改的情况下使用您的代码,如下所示:
Array.prototype.sum_cat = function (prop, cat, val) {
let total = 0,
_len = this.length;
for (let i = 0; i < _len; i++) {
if (this[i][cat] == val) { total += this[i][prop]; }
}
return total;
}
console.log('total 2: '+programs_array.sum_cat('target_revenue','category',2));