foreach 遍历
<!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>
//foreach 遍历
var arr = ["aaa","bbb","ccc","ddd"]
// 回调函数
arr.forEach(function(item,index,arr){
console.log(item,index,arr)
}
</script>
</body>
</html>
map映射
把arr里面的值都变成“xiaoming”
<!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>
//map映射 把arr里面的值都变成“xiaoming”
var arr = [1,2,3,4,5]
var arr2 = arr.map(function(item){
return "xiaoming"
})
console.log(arr2)
</script>
</body>
</html>
filter 过滤
过滤出大于200的数
<!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>
// filter 过滤
var arr = [100,200,300,400]
var arr2 = arr.filter(function(item){
return item>200
})
console.log(arr2)
</script>
</body>
</html>
过滤对象中price大于200的
// filter 过滤对象
var arr = [
{name:"aaa",price:100},
{name:"bbb",price:200},
{name:"ccc",price:300},
]
// var arr = [100,200,300,400]
var arr2 = arr.filter(function(item){
return item.price>200
})
console.log(arr2)
every
数组里每一个都要符合条件显示true,否则false
// every 每一个都要符合条件显示true,否则false
var arr = [80,90,92,94]
var arr2 = arr.every(function(item){
return item>=90
})
console.log(arr2)
some
some 只要一个满足条件就是true
// some 只要一个满足条件就是true
var arr = [80,10,20,30]
var arr2 = arr.some(function(item){
return item>=50
})
console.log(arr2)
</script>
find
只能找出符合条件的第一项
// find只能找出符合条件的第一项
var arr = [
{ name: "aaa", price: 100 },
{ name: "bbb", price: 200 },
{ name: "ccc", price: 300 },
{ name: "ddd", price: 100 }
]
var arr2 = arr.find(function(item){
return item.price===100
})
console.log(arr2)
reduce 叠加
// reduce 叠加
var arr = [1,2,3,4,5]
var arr2 = arr.reduce(function(prev,item){
return prev+item
},0)
console.log(arr2)
标签:200,arr,price,JavaScript,---,item,arr2,数组,var
From: https://blog.csdn.net/weixin_58694594/article/details/142406768