vue3 数据逻辑处理 将同一个 对应的id 的区分出来

先上代码

   const groupedOptions = {} // 使用对象来存储分组结果
       data.list.forEach(item => {
// 遍历 groupList.value,检查是否有匹配的 id
        groupList.value.forEach(group => {
           if (group.id === item.groupId) {
             // 如果 group.id 在 groupedOptions 中不存在,则初始化它
             if (!groupedOptions[group.groupName]) {
             groupedOptions[group.groupName] = []
             }
             // 将当前 item 添加到匹配的组中
             groupedOptions[group.groupName].push({
               value: item.id,
               label: item.nickName,
             })
           }
         })
       })
       // 将 groupedOptions 转换为 options.value 所需的格式
      options.value = Object.keys(groupedOptions).map(groupName => ({
         label: groupName,
        options: groupedOptions[groupName],
       }))

我这个主要处理这个问题 (groupList)

[
    {
        "id": "1",
        "groupName": "xxx",
        "userId": "1251139989602463744",
        "createTime": "1718594337191",
        "updateTime": "1718594337191",
        "channel": 3
    },
    {
        "id": "2",
        "groupName": "bbb",
        "userId": "1251139989602463744",
        "createTime": "1718441669851",
        "updateTime": "1718441669851",
        "channel": 3
    },
    {
        "id": "3",
        "groupName": "ccc",
        "userId": "1251139989602463744",
        "createTime": "1718441665340",
        "updateTime": "1718441665340",
        "channel": 3
    }
]

data.list

[
    {
        "id": "1252211435414130688",
        "nickName": "闪电票务",
        
        "groupId": "1",
        "customerId": "0",
     
        
    {
        "id": "1251590257989292032",
        "nickName": "xxx",
      
        "groupId": "1",
        
    },
 {
        "id": "1251590257989292032",
        "nickName": "ccc",
      
        "groupId": "3",
        
    },
   
 {
        "id": "1251590257989292032",
        "nickName": "xxx",
      
        "groupId": "2",
        
    },
   
 {
        "id": "1251590257989292032",
        "nickName": "xxx",
      
        "groupId": "2",
        
    },
   
 {
        "id": "1251590257989292032",
        "nickName": "xxx",
      
        "groupId": "3",
        
    },
   
   
  
]

把这个分组 一下 放在一起 变成这样的数据

[
      {
        label: 'Manager',
        options: [
          {
            value: 'jack',
            label: 'Jack',
          },
          {
            value: 'lucy',
            label: 'Lucy',
          },
        ],
      },
      {
        label: 'Engineer',
        options: [
          {
            value: 'yiminghe',
            label: 'Yiminghe',
          },
        ],
      },
    ]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值