const arr = [
{
title: 'test1',
id: 1,
children: [
{
title: 'test1-1',
id: 11,
children: [
{
title: 'test1-1-1',
id: 111,
children: [],
},
],
},
],
},
{
title: 'test1',
id: 1,
children: [
{
title: 'test1-1',
id: 11,
children: [
{
title: 'test1-1-2',
id: 112,
children: [],
},
],
},
],
},
{
title: 'test1',
id: 1,
children: [
{
title: 'test1-2',
id: 12,
children: [],
},
],
},
];
function handlerArrayMerge(value){
const tempIds = [],newArrs = [];
for(const item of value){
if(!tempIds.includes(item.id)){
tempIds.push(item.id);
newArrs.push(item);
}else{
for(const ele of newArrs){
if(ele.id === item.id){
ele.children = handlerArrayMerge(ele.children.concat(item.children));
}
}
}
}
return newArrs;
}