map(映射)
例1
let arr=[12,4,67]
let result=arr.map(item=>item*2) //箭头函数
console.log(result) //[24, 8, 134]
例2
let score=[12,67,99]
let result=score.map(item=>item>=60?"及格":"不及格") //箭头函数+if/else简写
console.log(result) //["不及格", "及格", "及格"]
reduce(汇总)
let arr=[12,69,180,8763]
let result=arr.reduce(function(tmp,item,index){ //tmp:12->81->261
return tmp+item //item:69->180->8763
}) //index:1->2->3
//return:81->261->9024
console.log(result) //9024
filter(过滤器)
例1
let arr=[12,5,8,99,27,36,75]
let result=arr.filter(item=>item>=30?true:false)
console.log(result) //[99,36,75]
例2
let arr=[
{name:"Li",price:100},
{name:"Ka",price:200},
{name:"Ii",price:700},
{name:"PY",price:400},
]
let result=arr.filter(item=>item.price>200?true:false)
console.log(result)//[{name:"Ii",price:700},{name:"PY",price:400}]
foreach(遍历,无返回值)
let arr=[12,5,8,99,27,36,75]
arr.forEach(item=>{console.log(item+1)})