ES6 数组map(映射)、reduce(汇总)、filter(过滤器)、forEach(循环迭代)
map(映射) #一个对一个
<script>
let arr=[2,4,7]
let result=arr.map(function (item) {
return item*2
});
alert(result)
</script>
<script>
let arr=[54,76,98,43]
let result=arr.map(item=>item>=60?'及格':'不及格');
alert(arr)
alert(result)
</script>
reduce(汇总) #一堆出来一个
<script>
let arr=[54,76,98,43]
let result=arr.reduce(function (tmp,item,index) {
if (index != arr.length-1){
return tmp+item
}else{
return (tmp+item)/arr.length
}
})
alert(result)
</script>
filter(过滤器)
<script>
let arr=[54,76,98,43,12]
let result=arr.filter(function (item) {
if(item%3==0){
return true
}else{
return false
}
})
alert(result)
</script>
forEach(循环迭代)
<script>
let arr=[54,76,98,43,12]
arr.forEach((item,index)=>{
alert(index+':'+item)
})
alert(result)
</script>