从一个数组中拿到另一个数组中的值

简介: 从一个数组中拿到另一个数组中的值

现在有两个数组,数组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;
  })
相关文章
|
19天前
|
存储 C语言
|
2月前
|
存储 搜索推荐 程序员
C++ 数组
C++ 数组
28 0
|
2月前
1-9 数组
1-9 数组
14 0
|
2月前
|
程序员 索引
C 中数组详解
C 中数组详解。
22 3
|
9月前
|
存储 C语言 索引
C 数组
C 数组。
28 0
|
2月前
数组练习2
数组练习2。
19 2
|
2月前
|
存储 JavaScript 前端开发
数组
数组
44 0
|
7月前
|
存储 编译器 程序员
数组详解
数组详解
53 0
|
8月前
|
机器学习/深度学习 Java
【数组的使用】
【数组的使用】
36 0
|
11月前
|
存储 IDE Linux