对象多级对象数组同级合并子节点
// 递归合并子节点函数
const mergeChildren = (data:columnObj[]) => {
const mergedData:columnObj[] = [];
for (let i=0; i<data.length; i++) {
const item = { ...data[i] };
if (item.children && item.children.length>0) {
const mergedItem = mergedData.find(merged => merged.title === item.title);
if (mergedItem) {
mergedItem.children!.push(...item.children);
} else {
mergedData.push(item);
}
} else {
mergedData.push(item);
}
// 递归处理子节点
for (let k = 0; k<mergedData.length; k++) {
if (mergedData[k].children && mergedData[k].children!.length > 0) {
mergedData[k].children = mergeChildren(mergedData[k].children!);
}
}
}
return mergedData;
};