首页 > 其他分享 >Vue循环实现累加的七种方法

Vue循环实现累加的七种方法

时间:2024-02-22 09:02:30浏览次数:23  
标签:Vue return 七种 price 累加 item books let total

<body>
    <div id="app">
        <h2>总价:{{totalPrice}}</h2>
    </div>
    <script>
        const vm = new Vue({
            el: "#app",
            data() {
                return {
                    books: [
                        { id: 110, name: "JavaScript从入门到入土", price: 119 },
                        { id: 111, name: "Java从入门到放弃", price: 80 },
                        { id: 112, name: "编码艺术", price: 99 },
                        { id: 113, name: "代码大全", price: 150 },
                    ]
                }
            },
            //原始方法
            /* computed:{
                totalPrice(){
                    let total = 0;
                    for(let i = 0;i < this.books.length;i++){
                        total += this.books[i].price;
                    }
                    return total;
                }
            } */
            
            //for...in可枚举的
            /* computed:{
                totalPrice(){
                    let total = 0;
                    for(let i in this.books){
                        total += this.books[i].price;
                    }
                    return total;
                }
            } */
 
            //for...of可迭代的
            /* computed:{
                totalPrice(){
                    let total = 0;
                    for(let item of this.books){
                        total += item.price;
                    }
                    return total;
                }
            } */
 
            // forEach
            /* computed:{
                totalPrice(){
                    let total = 0;
                    this.books.forEach(item => {
                        total += item.price;
                    });
                    return total;
                }
            } */
 
            //map方法,对数组元素进行操作,返回一个新的数组
            /* computed: {
                totalPrice() {
                    let total = 0;
                    this.books.map(item => {
                        total += item.price;
                    })
                    return total;
                }
            } */
            
            //对数组元素进行筛选,返回一个新的数组
            /* computed:{
                totalPrice(){
                    let total = 0;
                    this.books.filter(item=>{
                        total+=item.price;
                    })
                    return total;
                }
            } */
 
            //total初始值,没有初始值就是数组的第一个元素,item现在项,累加器。
            computed: {
                totalPrice() {
                    return this.books.reduce((total, item) => {
                        return  total + item.price
                    }, 0)
                }
            }
        })
    </script>
</body>

 

 

//js代码
<script>
    export default {
        components: {},
        props: [],
        data() {
            return {
                "amount": 1000,//模拟数据总金额
                "form": {
                    "items": []
                }
            }
        },
        computed: {},
        watch: {},
        created() {},
        beforeMount() {},
        mounted() {
            let list = [{//模拟数据源
                name: "和平区",
                index: 0,
                fund: 0,
                scale: 0
            }, {
                name: "沈河区",
                index: 1,
                fund: 0,
                scale: 0
            }]
            this.form.items = list
        },
        destroyed() {},
        methods: {
            commit() {
                //提交表单时 通过reduce函数计算各个区县的总金额  以及实现后续校验
                let total = this.form.items.reduce((sum, item) => sum + Number(item.fund || 0), 0);
                console.log(total)
            },
            //通过填入金额计算比例
            applyScale(index, fund) {
                this.form.items[index].scale = (fund / this.amount * 100).toFixed(2)
            },
        },
        config: {}
    }
</script>

 

const QualityData = ref([]);
    const QualitySum1 = ref();
    const QualitySum2 = ref();
    const get_QualityManagement = async () => {
        const param = {
            year: year,
        };
        const _data = await getQualityManagement(param);
        const {
            data
        } = _data;
        QualityData.value = data;
        QualitySum1.value = data.special.reduce((total, num) => {
            return total + num
        });
        QualitySum2.value = data.aclass.reduce((total, num) => {
            return total + num
        });
        //console.log('QualitySum1>>>' + JSON.stringify(QualitySum1));
    };

 

标签:Vue,return,七种,price,累加,item,books,let,total
From: https://www.cnblogs.com/Fooo/p/18026571

相关文章

  • ssts-hospital-web-master项目实战记录六:项目迁移方案大纲(html -> vue)
    记录时间:2024-02-21(一)公共资源部分Inc/cssInc/flashInc/imagesInc/jsInc/voice(二)页面部分1.主页及其组成(1)index.html->App.vue(2)MainPage*.html->views/main-page*MainPage1.html->views/main-page1MainPage2.html->views/main-page2MainPage3.html->......
  • Vue项目中关闭Eslint检查
    1.问题由于Eslint检查总是无法通过编译,在我们写一些简单的程序时显得十分麻烦,便想要关闭它2.解决这里有两种情况2.1使用WebStorm自带的Eslint导致的检查这种情况只要在WebStorm中的设置里关闭即可2.2Vue项目创建时选择了Eslint检查,生成了相应的js文件参考:vue关闭eslint(......
  • vue ui无效,没有打开可视化页面
    1.问题选择使用vueui打开可视化界面来设置Vue项目,但是并没有打开相应界面2.解决参考:vueui无效,没有打开可视化页面通过查找了资料后发现,原来要vue3.x版本以上才有vueui这个命令。而我当前的版本为2.9.6解决方法:先用npmuninstallvue-cli-g删除现在已有的vue(1.x和2.x......
  • Java人力资源管理系统源码(含数据库)-springboot+vue+mysql
    EHR人力资源管理系统是一种利用现代技术,如云计算、大数据等,来实现企业人力资源信息电子化、流程自动化的系统。它覆盖了人力资源管理的各个方面,从招聘、考勤、绩效到薪酬、社保公积金等,为企业提供一站式的解决方案。​1.招聘管理:-职位发布:系统支持在线发布职位信息,吸引候选人......
  • vue2.0和vue3.0在同一电脑上运行(超详细步骤)
    由于现在公司项目都是vue2.0项目,个人又需要3.0来学习。所以需要在同一电脑安装两个版本的vue。1.在创建vue2.0和vue3.0两个文件夹,并且局部安装 在vue2文件夹执行命令[email protected](版本根据自身来选择),[email protected]在vue3文件夹执行命令npmin......
  • Vue 学习笔记 3--live server
    liveserver插件在Vscode扩展插件中安装liveserver,启用扩展后展示如下所示: 单击GoLive,会运行启用网页,如下图所示: 注:使用http://127.0.0.1:5500/可查看网页下详细资源信息 ......
  • 对vue MVVM的探究
    对vueMVVM的探究这个问题是我在写blazor时想到的MVVM咱们都知道MVVM分了Model、ViewModel、View三层Model层是数据ViewModel层是给View层提供显示的数据和逻辑操作View层是界面显示在WPF里面是处理的很好,在XAML中DataContext是ViewModel,View可以直接绑定ViewModel的数据......
  • Vue 学习笔记2 -- 开发环境
    第一步:创建文件夹并在VScode中打开第二步:创建src目录+index.html第三步:https://v2.cn.vuejs.org/v2/guide/installation.html下载vue依赖文件,如下图所示: 第四步:引入vue.js,如下图所示: 第五步:运行+F12解决如下图问题 https://v2.cn.vuejs.org/v2/guide/instal......
  • vue3项目模板:新建一个vite+vue3项目,并做基础化建设
    原文地址:https://blog.csdn.net/weixin_43239880/article/details/130355138新建一个vite+vue3项目,并做基础化建设1.使用npmcreatvite@latest新建一个vue3项目2.生成git仓库3.将prettier的规则加入到eslint中(可选操作,建议有)4.添加commitLint(可选操作,建议有)5.加入UI组件库,以ele......
  • Vue3组合式API之getCurrentInstance详解
    Vue2中,可以通过this来获取当前组件实例; Vue3中,在setup中无法通过this获取组件实例,console.log(this)打印出来的值是undefined。在Vue3中,getCurrentInstance()可以用来获取当前组件实例  vue3官方文档解释let{proxy}=getCurrentInstance(); 在setup中分别打印下面......