Page({
data: {
carts:[
{id:1,title:'新鲜芹菜',image:'/image/s5.png',num:4,price:1.8}
{id:2,title:'素米 ',image:'/image/s6.png',num:1,price:12 ]},
onShow() {
},
})
Page({
data: {
carts:[
{id:1,title:'新鲜芹菜',image:'/image/s5.png',num:4,price:1.8}
{id:2,title:'素米 ',image:'/image/s6.png',num:1,price:12 ]},
onShow() {
},
})
用reduce,参考这个,取carts的值
const carts = [{ id: 1, title: '新鲜芹菜', image: '/image/s5.png', num: 4, price: 1.8 },{ id: 2, title: '素米 ', image: '/image/s6.png', num: 1, price: 12}]
let $total = 0
$total = carts.reduce(function (total, currentValue, currentIndex, arr) {
return currentValue['num'] ? total + currentValue['num'] * currentValue['price'] : total
},0)
console.log($total)