weixin_58946818 2022-01-08 23:30 采纳率: 33.3%
浏览 92

微信小程序商城购物车计算总价逻辑怎么写呀?

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() {
    
  },

})

  • 写回答

2条回答 默认 最新

  • 你好!机器人 2022-01-09 00:01
    关注

    用reduce,参考这个,取carts的值

    img

            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)
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 1月8日

悬赏问题

  • ¥15 sql server 2012的下载出错
  • ¥15 图像识别用户软件开发
  • ¥20 类原生rom lineageos
  • ¥15 有没有会做中专,云计算,卷子的,有偿一百块
  • ¥15 HC32串口DMA循环发送数据
  • ¥15 Uni-App实现飞书授权登陆
  • ¥50 Qt应用中如何通过代码打开开发者工具devtools
  • ¥20 mpp硬解码h264转为yuv
  • ¥40 怎样批量对比两个数据库的表差异
  • ¥15 lettuce连接哨兵redis,主从切换异常