ES6数组find实现嵌套数据匹配

业务场景:

有两个数据源A和B,格式为数组包裹的对象列表,对象里面有标识字段“user_id”

  infoA = [
          {
            user_id: 4,
            org_id: 9,
            org_no: "1212121",
            name: "杨赛",
            sex: "男",
            cardno: "142729199611101418",
            phone: "13522687472",
            nation: "汉",
            native: "江苏省南京市",
            birthday: "-",
            health: "其他",
            marital_status: "其他",
            classification: "未定义",
            face: "其他",
            education: "其他",
            expand: "",
            status: "其他",
            is_delete: 0,
            updated_at: "2021-12-01",
            created_at: "2021-11-25",
            org_name: "1212121",
          },
          {
            user_id: 8,
            org_id: 1,
            org_no: "01",
            name: "李歌女",
            sex: "男",
            cardno: "142719199711191257",
            phone: "13522681234",
            nation: "汉",
            native: "江苏省南京市",
            birthday: "-",
            health: "其他",
            marital_status: "其他",
            classification: "未定义",
            face: "其他",
            education: "其他",
            expand: "123",
            status: "其他",
            is_delete: 0,
            updated_at: null,
            created_at: "2021-11-26",
            org_name: "连云区一中",
          },
          {
            user_id: 9,
            org_id: 1,
            org_no: "01",
            name: "黄春花",
            sex: "男",
            cardno: "321719199711191257",
            phone: "13522685634",
            nation: "汉",
            native: "广东省珠海市",
            birthday: "-",
            health: "其他",
            marital_status: "其他",
            classification: "未定义",
            face: "其他",
            education: "其他",
            expand: "fff",
            status: "其他",
            is_delete: 0,
            updated_at: null,
            created_at: "2021-12-29",
            org_name: "连云区一中",
          },
          {
            user_id: 11,
            org_id: 1,
            org_no: "01",
            name: "李歌女2",
            sex: "男",
            cardno: "142719199711191258",
            phone: "13522681234",
            nation: "汉",
            native: "江苏省南京市",
            birthday: "-",
            health: "其他",
            marital_status: "其他",
            classification: "未定义",
            face: "其他",
            education: "其他",
            expand: "123",
            status: "其他",
            is_delete: 0,
            updated_at: null,
            created_at: null,
            org_name: "连云区一中",
          },
        ],
   infoB = [
          {
            user_id: 4,
            org_id: 9,
            org_no: "1212121",
            name: "杨赛",
            sex: 1,
            cardno: "142729199611101418",
            phone: "13522687472",
            nation: "汉",
            native: "江苏省南京市",
            birthday: "19961110",
            health: 0,
            marital_status: 0,
            classification: 0,
            face: 0,
            education: 1,
            expand: "",
            status: 0,
            is_delete: 0,
            updated_at: "1638343691",
            created_at: "1637823725",
          },
          {
            user_id: 8,
            org_id: 1,
            org_no: "01",
            name: "李歌女",
            sex: 2,
            cardno: "142719199711191257",
            phone: "13522681234",
            nation: "汉",
            native: "江苏省南京市",
            birthday: "19971119",
            health: 1,
            marital_status: 1,
            classification: 1,
            face: 1,
            education: 1,
            expand: "123",
            status: 0,
            is_delete: 0,
            updated_at: null,
            created_at: "1637893773",
          },
          {
            user_id: 9,
            org_id: 1,
            org_no: "01",
            name: "黄春花",
            sex: 2,
            cardno: "321719199711191257",
            phone: "13522685634",
            nation: "汉",
            native: "广东省珠海市",
            birthday: "19971119",
            health: 1,
            marital_status: 1,
            classification: 1,
            face: 1,
            education: 1,
            expand: "fff",
            status: 0,
            is_delete: 0,
            updated_at: null,
            created_at: "1640719204",
          },
          {
            user_id: 11,
            org_id: 1,
            org_no: "01",
            name: "李歌女2",
            sex: 2,
            cardno: "142719199711191258",
            phone: "13522681234",
            nation: "汉",
            native: "江苏省南京市",
            birthday: "19971119",
            health: 1,
            marital_status: 1,
            classification: 1,
            face: 1,
            education: 1,
            expand: "123",
            status: 0,
            is_delete: 0,
            updated_at: null,
            created_at: null,
          },
        ];

业务需求:

点击数据源A中的某条数据时,获取对应B中的数据。

ES6方法:

Array.find((value,index,arr)=>{ return 条件表达式;})

value:每一次迭代查找的数组元素。

index:每一次迭代查找的数组元素索引。

arr:被查找的数组。

返回符合条件表达式的第一个value

Array.findIndex((value,index,arr)=>{ return 条件表达式;})

返回符合条件表达式的第一个value的index

实现代码:

      // 假设被点击的数据是下面这条,我们用变量example接收一下
      let example = {
        user_id: 11,
        org_id: 1,
        org_no: "01",
        name: "李歌女2",
        sex: "男",
        cardno: "142719199711191258",
        phone: "13522681234",
        nation: "汉",
        native: "江苏省南京市",
        birthday: "-",
        health: "其他",
        marital_status: "其他",
        classification: "未定义",
        face: "其他",
        education: "其他",
        expand: "123",
        status: "其他",
        is_delete: 0,
        updated_at: null,
        created_at: null,
        org_name: "连云区一中",
      };
      //   在我们需要的数据源中进行查找,我们最终需要的是B中的数据,所以就在B中查找,将查到的数据用变量target接收
      let target = infoB.find((value, index, arr) => {
        return value.user_id === example.user_id; // 如果B中元素的user_id和example中的user_id相等,那么就返回该元素并结束find
      });

     在浏览器控制台输入target查看结果,符合需求。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值