gsdev99,
пока неизвестно, как формируется новое type, вариант такой
const widgets = [
{
type: "Title",
data: {
title: "title1",
subTitle: "subTitle1"
}
},
{
type: "Title",
data: {
title: "title2",
subTitle: "subTitle2"
}
},
{
type: "Title",
data: {
title: "title3",
subTitle: "subTitle3"
}
},
{
type: "New",
data: {
title: "new title1",
subTitle: "new subTitle1"
}
},
{
type: "New",
data: {
title: "new title2",
subTitle: "new subTitle2"
}
},
{
type: "NewTitle",
data: {
title: "NewTitle title1",
subTitle: "NewTitle subTitle1"
}
},
];
const create = arr => {
const map = new Map;
arr.forEach(o => {
let {type, data} = o;
//type = "Section" + type;
if(map.has(type)) map.get(type).children.push(data);
else map.set(type, {type, children : [data]})
})
return [...map.values()]
};
const widgetsTransform = create(widgets)
console.log(widgetsTransform)