Исходный массив:
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"
}
},
]