根据相同名称进行分类 并增加parent 树型数据

const data=[
  { id: 14, text: "设备名称1", start_date: "03-04-2023 00:00", progress: 0.5, duration: 11 },
  { id: 5, text: "设备名称1", start_date: "03-04-2023 00:00", duration: 1, progress: 0, },
  { id: 6, text: "设备名称1", start_date: "05-04-2023 00:00", duration: 2, progress: 0, },
  { id: 7, text: "设备名称1", start_date: "08-04-2023 00:00", duration: 1, progress: 0, },
  { id: 8, text: "设备名称1", start_date: "10-04-2023 00:00", duration: 4, progress: 0, },
  { id: 13, text: "设备名称2", start_date: "03-04-2023 00:00",  progress: 0.5, duration: 11 },
  { id: 17, text: "设备名称2", start_date: "03-04-2023 00:00", duration: 1, progress: 0.3, priority: '1', color: '#2F80ED' },
  { id: 18, text: "设备名称2", start_date: "05-04-2023 00:00", duration: 2, progress: 0, color: '#2F80ED' },
  { id: 19, text: "设备名称2", start_date: "08-04-2023 00:00", duration: 1, progress: 0.6, color: '#f7cfd3' },
  { id: 20, text: "设备名称2", start_date: "10-04-2023 00:00", duration: 4, progress: 0.5, color: '#f7cfd3' },]

根据相同的text 名称分类 并且分类出来的第一个数据里面增加render: "split", 属性,剩余的增加parent:id,其中id值为分类出来的第一个id的值
 

// 创建一个对象,用于存储分类后的数据
  const classifiedData = {};

  // 遍历原始数据数组
  for (const item of data) {
      const { id, text } = item;

      // 如果分类对象中不存在该分类,则创建一个新的数组
        if (!classifiedData[text]) {
            classifiedData[text] = [];
           }
           // 将数据项添加到对应分类的数组中
            classifiedData[text].push(item);
         }

         // 遍历分类后的数据
         for (const category in classifiedData) {
              const categoryData = classifiedData[category];
              // 给分类的第一个数据项添加render属性
              const firstItem = categoryData[0];
              firstItem.render = "split";

              // 给剩余数据项添加parent属性,值为分类的第一个id
              for (let i = 1; i < categoryData.length; i++) {
                 categoryData[i].parent = firstItem.id;
             }
          }
console.log(classifiedData)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值