当前位置: 首页 > Web前端 > vue.js

数组遍历方法

时间:2023-04-01 01:28:48 vue.js

varbooks=[{name:'西游记',author:'吴承恩',price:58,count:5},{name:'三人行演义》,作者:'罗贯中',单价:68,张数:6},{name:'水浒传',作者:'施耐庵',单价:48,张数:8},{name:'红梦Mansions',author:'曹雪芹',price:78,count:10}](1)通过for循环计算总价lettotalPrice=0for(leti=0;i{totalPrice+=item.price*item.count})(5)map返回一个新数组lettotalPrice=0this.books.map(item=>{returntotalPrice+=item.price*item.count})(6)reduce函数的作用:汇总数组中的所有内容