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)