var list = [
{
title: '这是答案0-1',
type: 'y'
},
{
title: '这是答案0-2',
type: 'y'
},
{
title: '这是问题1',
type: 'x'
},
{
title: '这是答案1',
type: 'y'
},
{
title: '这是答案2',
type: 'y'
},
{
title: '这是答案3',
type: 'y'
},
{
title: '这是答案4',
type: 'y'
},
{
title: '这是问题2',
type: 'x'
},
{
title: '这是答案2-1',
type: 'y'
},
{
title: '这是答案2-2',
type: 'y'
},
{
title: '这是答案2-3',
type: 'y'
},
{
title: '这是答案2-4',
type: 'y'
},
{
title: '这是问题3',
type: 'x'
},
{
title: '这是答案3-1',
type: 'y'
}
]
function fn() {
let arr_ind = []
for (let x = 0; x < list.length; x++) {
if (list[x].type === 'x') {
arr_ind.push(x)
}
}
let arr = []
for (let y = 0; y < arr_ind.length; y++) {
if (arr_ind[y] !== 0 && y === 0) {
arr.push({
title: null,
list: list.slice(0, arr_ind[y])
})
} else {
arr.push({
title: list[arr_ind[y]].title,
list: list.slice(arr_ind[y] + 1, arr_ind[y + 1])
})
}
}
}
fn()
console