filter()函数是过滤函数,将符合条件的数据返回出来,当return ture;时表示该数据符合过滤条件。
let zq=["知青","知青学习"]; let newArray=zq.filter(function(value,index,arr){ console.log(value,index,arr); return true;//返回值为true时,表示要这个值 }); console.log(newArray);
案例:在所有的课程中过滤出js课程
let lesson=[{name:"学习js第一课",category:"js"},{name:"学习js第二课",category:"js"},{name:"学习python第一课",category:"python"}]; let res=lesson.filter(function(value){ return value.category=="js"; }); console.table(res);