Vue2.0开发之——数组中的方法(43)

编程入门 行业动态 更新时间:2024-10-27 10:22:43

Vue2.0开发之——数<a href=https://www.elefans.com/category/jswz/34/1771283.html style=组中的方法(43)"/>

Vue2.0开发之——数组中的方法(43)

一 概述

  • some循环
  • every循环
  • reduce的用法

二 some循环

2.1 使用array的forEach方法—一旦循环开始,无法在中间停止

代码

const array=['红','黄','蓝','绿']
array.forEach((item,index)=>{console.log('ok')if(item==='蓝'){console.log(index)return;}
})

打印结果(所有元素都循环到了—到蓝结束)

3 ok
2
ok

2.2 使用some方法—找到对应项后,通过return true来终止some循环

代码

const array=['红','黄','蓝','绿']
array.some((item,index)=>{console.log('ok')if(item==='蓝'){console.log(index)return true}})

打印结果

3 ok
2

三 every循环—判断数组中,水果是否被全选了

代码

const arr=[{id:1,name:'西瓜',state:true},{id:2,name:'榴莲',state:true},{id:3,name:'草莓',state:true},]const result=arr.every(item=>item.state)
console.log(result)

打印结果

true

四 reduce的基本用法

4.1 计算选中水果的价格—filter,forEach

代码

const arr=[{id:1,name:'西瓜',state:true,price:10,count:1},{id:2,name:'榴莲',state:false,price:80,count:2},{id:3,name:'草莓',state:true,price:20,count:3},]let amt=0 //总价
arr.filter(item=>item.state).forEach(item=>{amt+=item.price*item.count
})
console.log(amt)

打印结果

70

4.2 计算选中水果的价格—filter,reduce

代码

const arr=[{id:1,name:'西瓜',state:true,price:10,count:1},{id:2,name:'榴莲',state:false,price:80,count:2},{id:3,name:'草莓',state:true,price:20,count:3},]
const result = arr.filter(item=>item.state).reduce((amt,item)=>{return amt+=item.price*item.count
},0)
console.log(result)

reduce方法说明:

reduce((累加结果,当前循环项)=>{},初始值)

4.3 reduce的简化写法

const result = arr.filter(item=>item.state).reduce((amt,item)=>amt+=item.price*item.count,0)

更多推荐

Vue2.0开发之——数组中的方法(43)

本文发布于:2024-02-25 20:14:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!