1、数组的循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arr=['小红','小绿','小蓝']
arr.some((item,index)=>{
console.log('ok')
if(item==='小绿'){
return true
}
})
</script>
</body>
</html>
2、数组的every方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arr=[
{id:1,name:'西瓜',state:true},
{id:2,name:'草莓',state:true},
{id:3,name:'木瓜',state:true},
]
const res=arr.every(item=>item.state===true)
console.log(res)
</script>
</body>
</html>
3、数组的reduce方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arr=[
{id:1,name:'西瓜',state:true,price:10,count:1},
{id:2,name:'草莓',state:true,price:80,count:2},
{id:3,name:'木瓜',state:false,price:20,count:3},
]
const res=arr.filter(item=>item.state).reduce((amt,item)=>{
return amt+=item.price*item.count
},0)
console.log(res)
const res=arr.filter(item=>item.state).reduce((amt,item)=>amt+=item.price*item.count,0)
</script>
</body>
</html>