js 计算对象数组中某个属性合计

countTotal调用示例:

let arr = [
	{id: 0, price: 199.88},
	{id: 1, price: 299.88},
	{id: 2, price: 399.88}
];
console.log(countTotal(arr, 'price')); //899.64
//计算对象数组中某个属性合计
function countTotal(arr, keyName) {
	let $total = 0;
	$total = arr.reduce(function (total, currentValue, currentIndex, arr){
	    return currentValue[keyName] ? (total + currentValue[keyName]) : total;
	}, 0);
	return $total;
}

相关文章:

  • 2021-11-09
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-25
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案