达者均为吾师

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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;
}

  

posted on 2022-03-26 10:08  达者均为师  阅读(846)  评论(0编辑  收藏  举报