现在有两个数组,数组a中有两个目标id值,数组b中是原数组,需求:想要从b数组中拿到含有a数组中id的值
const a =[1,5]
const b =[
{
id:1,sector:'技术部',name:'高工',createTime:'2022-02-10'},
{
id:2,sector:'测试部',name:'张工',createTime:'2022-02-10'},
{
id:3,sector:'技术部',name:'李工',createTime:'2022-02-10'},
{
id:4,sector:'测试部',name:'王工',createTime:'2022-02-10'},
{
id:5,sector:'技术部',name:'高工',createTime:'2022-02-10'},
]
const c=[]
解法一:forEach方法
a.forEach((pa)=>{
b.forEach((pb)=>{
if(pa === pb){
c.push(pb.name)
}
})
})
解法二:filter
const c = b.filter((p)=>{
if(a.includes(p.id)){
return true;
}
return false;
})