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 widgetsTransform = [
{
type: "SectionTitle",
children: [
{
type: "Title",
data: {
title: "title1",
subTitle: "subTitle1"
}
},
{
type: "Title",
data: {
title: "title2",
subTitle: "subTitle2"
}
},
{
type: "Title",
data: {
title: "title3",
subTitle: "subTitle3"
}
}
]
},
{
type: "SectionNew",
children: [
{
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 widgetsTransform = Object
.values(widgets.reduce((acc, n) => ((acc[n.type] = acc[n.type] ?? []).push(n), acc), {}))
.map(n => n.length > 1 ? ({ type: `Section${n[0].type}`, children: n }) : n[0]);const widgetsTransform = widgets
.reduce((acc, n, i, a) => (
(i && n.type === a[~-i].type) || acc.push([]),
acc[~-acc.length].push(n),
acc
), [])
.map(n => ~-n.length ? ({ type: 'Section' + n[0].type, children: n }) : n[0]);