const data = [
{
type: 'Question',
data: {
title: 'Question1 title',
subTitle: 'Question1 subTitle'
}
},
{
type: 'Question',
data: {
title: 'Question2 title',
subTitle: 'Question2 subTitle'
}
},
{
type: 'SectionHeader',
data: {
title: 'title1'
}
},
{
type: 'W',
data: {
title: 'W1 title',
subTitle: 'W1 subTitle'
},
properties: {
isExpanded: true
}
},
{
type: 'W',
data: {
title: 'W2 title',
subTitle: 'W2 subTitle'
}
},
{
type: 'SectionHeader',
data: {
title: 'title2'
}
}
]
[
{
type: 'SectionQuestion',
children: [
{
data: {
title: 'title',
subTitle: 'subTitle'
}
},
{
data: {
title: 'title',
subTitle: 'subTitle'
}
}
]
},
{
type: 'SectionHeader',
data: {
title: 'title1'
}
},
{
type: 'SectionW',
children: [
{
data: {
title: 'W1 title',
subTitle: 'W1 subTitle'
},
properties: {
isExpanded: true
}
},
{
data: {
title: 'W2 title',
subTitle: 'W2 subTitle'
}
}
]
},
{
type: 'SectionHeader',
data: {
title: 'title2'
}
}
]
const arrType = ['Question', 'W']
function f(arr, arrType) {
let res = []
let isPush = true
let widgetData = {
type: '',
children: []
}
arr.forEach(item => {
let { type } = item
if (arrType.includes(type)) {
widgetData.type = `Section${type}`
widgetData.children.push({
data: item.data,
properties: item.properties
})
if (isPush) {
res.push(widgetData)
isPush = false
}
} else {
res.push(item)
widgetData = {
type: '',
children: []
}
isPush = true
}
})
return res
}
console.log(f(data, arrType))
const transform = data => {
const res = data.reduce((agg, item) => {
item = {...item}
if (agg[agg.length - 1]?.type !== item.type)
agg.push({ type: item.type, children: [] })
agg[agg.length - 1].children.push((delete item.type, item))
return agg
})
res.filter(v => !v.type.startsWith('Section')).forEach(v => {
v.type = 'Section' + v.type
})
return res
}
const newData = data
.reduce((acc, { type, ...n }) => (
(acc[acc.length - 1]?.[0] !== type) && acc.push([ type, [] ]),
acc[acc.length - 1][1].push(n),
acc
), [])
.map(([ type, children ]) => children.length > 1
? { type: `Section${type}`, children }
: { type, ...children[0] }
);