js过滤器及累加器reduce的运用

<template>
    <view class="">
        <view class="inP" v-for="(item,index) in items" :key="index">
                <text>{{item.name}}</text>
                <view class="" style="margin-left: 20rpx;display: flex; align-items: center;">
                    <button type="default" @click="item.value -= 1" :disabled="item.value <= 0">-</button>
                    <text>{{item.value}}</text>
                    <button @click="item.value+=1">+</button>
                </view>
                <view class="" style="margin-left: 20rpx ;">
                    <text>{{item.prise | prisf(1) }}</text>
                </view>
                <view class="" style="margin-left: 20rpx ;">
                    <text>{{item.value*item.prise | prisf(2) }}</text>
                </view>
        </view>
        
        
        <view class="" style="margin-left:35rpx;">
            <text>总数量{{total}}件</text>
        </view>
        <view class="" style="margin-left:35rpx; margin-top: 10rpx">
            <text>总价格{{toprise | prisf(2)}}</text>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                   items:[
                       {
                           id:1,
                           name:"桃子",
                           prise:22,
                           value:1,
                           sum:''
                       },
                      {
                           id:1,
                           name:"粒子",
                           prise:10,
                           value:1,
                           sum:''
                      }, 
                   ],
                    }
        },
        
        // 过滤器
        filters:{
            // 局部
            prisf(data,n){
                // 添加人民币符号以及2位小数点
                return '¥' +data.toFixed(n)
            }
        },
            
        computed:{
            // 计算总数量
            total(){
                // var n = 0;
                // this.items.forEach(function(item){
                //     n+=item.value
                // });
                // return n;
                
                // reduce 累加器
                // total 计算结束后的返回值,cur 为当前值,为初始化
                return this.items.reduce((total,cur) => total+cur.value,0);
            },
            
            toprise(){
                // 总价格为数量乘以价格,cur.value*cur.prise
                return this.items.reduce((total,cur) => total + cur.value*cur.prise,0);
            }
        },
        
        onLoad() {

        },
        methods: {

        }
    }
</script>

<style>
    
    .inP{
        width: 90%;
        margin-left: 50%;
        transform: translateX(-50%);
        padding: 20rpx;
        display: flex;
        align-items: center;
}
</style>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值