Vue——02-02计算属性的复杂使用以及七种求和方法

简介: 计算属性的复杂使用以及七种求和方法

上一章讲述了计算属性的基本使用,这里说一下复杂使用

怎么使用计算属性computed:计算出所有books的总价格Price。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>计算属性的复杂使用</title>
</head>
<body>
    <div id="app">
        <h2>总价格:{{totalPrice}}</h2>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                books: [
                    { id: 110, name: "JavaScript从入门到入土", price: 119 },
                    { id: 111, name: "Java从入门到放弃", price: 80 },
                    { id: 112, name: "编码艺术", price: 99 },
                    { id: 113, name: "代码大全", price: 150 },
                ]
            },
        })
    </script>
</body>
</html>

20210923164423117.png

首先写入

computed: {
    totalPrice:function() {
    //写出七种方法
}
}

一共有七种求和方法:

第一种:利用for循环遍历

totalPrice:function() {
                     let total = 0;
                     for(let i = 0; i<this.books.length;i++){
                         total += this.books[i].price;
                     }
                     return total;
                 }

20210923164845197.png

第二种:forEach

totalPrice: function () {
                     let total = 0;
                     this.books.forEach((item) => {
                         total += item.price
                     })
                     return total;
                 }

第三种:for in

totalPrice: function () {
                    let total = 0;
                    for (let i in this.books){
                        total += this.books[i].price
                    }
                    return total;
                }

第四种:for of

totalPrice: function () {
                    let total = 0;
                    for(let value of this.books){
                        total += value.price
                    }
                    return total
                }

第五种:map

totalPrice: function () {
                    this.books.map((item)=>{
                        total += item.price
                    })
                    return total;
                }

第六种:filter

totalPrice: function () {
                    let total = 0;
                    this.books.filter((item)=>{
                        console.log(item);
                        total += item.price
                    })
                    return total;
                }

第七种:reduce

totalPrice: function () {       
                    return this.books.reduce((currentTotal,item)=>{
                        console.log(item);
                        return currentTotal + item.price
                    },0)
                }

简写:

totalPrice: function () {       
                    return this.books.reduce((currentTotal,item)=>+item.price)
                }

下面是完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>计算属性的复杂使用</title>
</head>
<body>
    <div id="app">
        <h2>总价格:{{totalPrice}}</h2>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                books: [
                    { id: 110, name: "JavaScript从入门到入土", price: 119 },
                    { id: 111, name: "Java从入门到放弃", price: 80 },
                    { id: 112, name: "编码艺术", price: 99 },
                    { id: 113, name: "代码大全", price: 150 },
                ]
            },
            computed: {
                //1、 for循环第一种方法
                 totalPrice:function() {
                     let total = 0;
                     for(let i = 0; i<this.books.length;i++){
                         total += this.books[i].price;
                     }
                     return total;
                 }
                //2、 forEach
                /*  totalPrice: function () {
                     let total = 0;
                     this.books.forEach((item) => {
                         total += item.price
                     })
                     return total;
                 } */
                //3、 for in
               /*  totalPrice: function () {
                    let total = 0;
                    for (let i in this.books){
                        total += this.books[i].price
                    }
                    return total;
                } */
                //4、 for of
                /* totalPrice: function () {
                    let total = 0;
                    for(let value of this.books){
                        total += value.price
                    }
                    return total
                } */
                //5、 map
                /* totalPrice: function () {
                    this.books.map((item)=>{
                        total += item.price
                    })
                    return total;
                } */
                //6、 filter
                /* totalPrice: function () {
                    let total = 0;
                    this.books.filter((item)=>{
                        console.log(item);
                        total += item.price
                    })
                    return total;
                } */
                //7、 reduce
               /*  totalPrice: function () {       
                    return this.books.reduce((currentTotal,item)=>{
                        console.log(item);
                        return currentTotal + item.price
                    },0)
                } */
               /*  totalPrice: function () {       
                    return this.books.reduce((currentTotal,item)=>+item.price)
                } */
            }
        })
    </script>
</body>
</html>

输出的price总和应为:448

20210923165203648.png



相关文章
|
1天前
|
JavaScript 开发工具 git
Vue 入门系列:.env 环境变量
Vue 入门系列:.env 环境变量
7 1
|
2天前
|
缓存 监控 JavaScript
探讨优化Vue应用性能和加载速度的策略
【5月更文挑战第17天】本文探讨了优化Vue应用性能和加载速度的策略:1) 精简代码和组件拆分以减少冗余;2) 使用计算属性和侦听器、懒加载、预加载和预获取优化路由;3) 数据懒加载和防抖节流处理高频事件;4) 图片压缩和选择合适格式,使用CDN加速资源加载;5) 利用浏览器缓存和组件缓存提高效率;6) 使用Vue Devtools和性能分析工具监控及调试。通过这些方法,可提升用户在复杂应用中的体验。
9 0
|
2天前
|
JavaScript
vue知识点
vue知识点
10 0
|
2天前
|
JavaScript 前端开发
vue(1),小白看完都会了
vue(1),小白看完都会了
|
2天前
|
JavaScript 数据库
ant design vue日期组件怎么清空 取消默认当天日期
ant design vue日期组件怎么清空 取消默认当天日期
|
2天前
|
JavaScript C++
vue高亮显示组件--转载
vue高亮显示组件--转载
8 0
|
2天前
|
JavaScript 前端开发 数据安全/隐私保护
揭秘Vue中v-model的内部工作机制
揭秘Vue中v-model的内部工作机制
|
2天前
|
JavaScript 前端开发 定位技术
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
|
2天前
|
JavaScript
Vue中避免滥用this去读取data中数据
Vue中避免滥用this去读取data中数据
|
2天前
|
JavaScript
vue中使用pinia及持久化
vue中使用pinia及持久化
5 0