<script th:inline="javascript">
$.get("http://mall.com/api/ware/wareinfo/fare?attrId=" + attrId, function (data) {
// TODO:保留两位小数toFixed(2)
$("b").text((data.data/100).toFixed(2));
let item = [[${orderConfirmData.items}]];
//TODO:map+reduce组合遍历求和
let add = $.map(item,function (v) {
return v.totalPrice;
}).reduce(function(total, currentValue, currentIndex){
return total+currentValue;
},parseFloat($("b").text()));//TODO:parseFloat字符串转为浮点数
</script>
语法
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
注意:应先转为array再进行reduce求和
参数 | 描述 |
---|
function(total,currentValue, index,arr) | 必需。用于执行每个数组元素的函数。 函数参数:
参数 | 描述 |
---|
total | 必需。初始值, 或者计算结束后的返回值。 | currentValue | 必需。当前元素 | currentIndex | 可选。当前元素的索引 | arr | 可选。当前元素所属的数组对象。 |
|
initialValue | 可选。传递给函数的初始值 |