如何处理这样一个数据?

sweepPaperTree: [

{

title: '一、基础(40分) (共5题,共40.0分)',

children: [

{

title: '1.填空题(8分)',

idProp: 'questionId',

code: 'completion',

blankNum: 4,

},

{

title: '2.单选题(8分)',

idProp: 'questionId',

code: 'single_choice',

},

{

title: '3.判断题(8分)',

idProp: 'questionId',

code: 'judgment',

},

{

title: '4.单选题(8分)',

idProp: 'questionId',

code: 'single_choice',

},

{

title: '5.多选题(8分)',

idProp: 'questionId',

code: 'multiple_choice',

},

{

title: '6.问答题(8分)',

idProp: 'questionId',

code: 'subjectivity',

},

{

title: '7.判断题(8分)',

idProp: 'questionId',

code: 'judgment',

},

{

title: '8.多选题(8分)',

idProp: 'questionId',

code: 'multiple_choice',

},

],

},

{

title: '二、阅读(40分) (共5题,共40.0分)',

children: [

{

title: '6.复合题(40分)',

idProp: 'questionId',

code: 'complex',

children: [

{

title: '6.(1)填空题(8分)',

idProp: 'questionItemId',

code: 'completion',

blankNum: 4,

showBorder: false,

},

{

title: '6.(2)单选题(8分)',

idProp: 'questionItemId',

code: 'single_choice',

showBorder: false,

},

{

title: '6.(3)问答题(8分)',

idProp: 'questionItemId',

code: 'subjectivity',

},

{

title: '6.(4)多选题(8分)',

idProp: 'questionItemId',

code: 'multiple_choice',

showBorder: false,

},

{

title: '6.(5)判断题(8分)',

idProp: 'questionItemId',

code: 'judgment',

showBorder: false,

},

],

},

],

},

{

title: '三、写作(50分) (共1题,共50.0分)',

children: [

{

title: '7.问答题(50分)',

idProp: 'questionId',

code: 'subjectivity',

},

],

},

],

上分是原始数据,如何转换为下方的数据格式呢

sweepPaperTree: [

{

title: '一、基础(40分) (共5题,共40.0分)',

children: [

{

title: '1.填空题(8分)',

idProp: 'questionId',

code: 'completion',

blankNum: 4,

},

[{

title: '2.单选题(8分)',

idProp: 'questionId',

code: 'single_choice',

},

{

title: '3.判断题(8分)',

idProp: 'questionId',

code: 'judgment',

},

{

title: '4.单选题(8分)',

idProp: 'questionId',

code: 'single_choice',

},

{

title: '5.多选题(8分)',

idProp: 'questionId',

code: 'multiple_choice',

}],

{

title: '6.问答题(8分)',

idProp: 'questionId',

code: 'subjectivity',

},

[{

title: '7.判断题(8分)',

idProp: 'questionId',

code: 'judgment',

},

{

title: '8.多选题(8分)',

idProp: 'questionId',

code: 'multiple_choice',

}],

],

},

{

title: '二、阅读(40分) (共5题,共40.0分)',

children: [

{

title: '6.复合题(40分)',

idProp: 'questionId',

code: 'complex',

children: [

{

title: '6.(1)填空题(8分)',

idProp: 'questionItemId',

code: 'completion',

blankNum: 4,

showBorder: false,

},

{

title: '6.(2)单选题(8分)',

idProp: 'questionItemId',

code: 'single_choice',

showBorder: false,

},

{

title: '6.(3)问答题(8分)',

idProp: 'questionItemId',

code: 'subjectivity',

},

[{

title: '6.(4)多选题(8分)',

idProp: 'questionItemId',

code: 'multiple_choice',

showBorder: false,

},

{

title: '6.(5)判断题(8分)',

idProp: 'questionItemId',

code: 'judgment',

showBorder: false,

}],

],

},

],

},

{

title: '三、写作(50分) (共1题,共50.0分)',

children: [

{

title: '7.问答题(50分)',

idProp: 'questionId',

code: 'subjectivity',

},

],

},

],

具体规则的是这样的:主要区分code为single_choice、multiple_choice、judgment且若下一题也是这三者之一的话就归到一个数组中(必须是连续的单选多选判断),如果是其他类型的题型还是原位置即可


回答:

function transformTree(list) {

const CODES = new Set(['single_choice', 'multiple_choice', 'judgment']);

return list.reduce((res, v, i) => {

if(v.children) v.children = transformTree(v.children);

const last = res[res.length-1];

if(last && CODES.has(list[i-1].code) && CODES.has(v.code)) {

if(Array.isArray(last)) last.push(v);

else res[res.length-1] = [last, v];

return res;

}

res.push(v);

return res;

}, [])

}


回答:

下面有chatgpt协助完成:

function transformPaperTree(paperTree) {

return paperTree.map(section => {

if (section.children) {

section.children = section.children.map(question => {

if (question.children) {

question.children = transformPaperTree(question.children);

}

return question;

});

}

return section;

});

}

// 使用示例

var paperTree = [

{

title: '一、基础(40分) (共5题,共40.0分)',

children: [

{

title: '1.填空题(8分)',

idProp: 'questionId',

code: 'completion',

blankNum: 4,

},

{

title: '2.单选题(8分)',

idProp: 'questionId',

code: 'single_choice',

},

{

title: '3.判断题(8分)',

idProp: 'questionId',

code: 'judgment',

},

{

title: '4.单选题(8分)',

idProp: 'questionId',

code: 'single_choice',

},

{

title: '5.多选题(8分)',

idProp: 'questionId',

code: 'multiple_choice',

},

{

title: '6.问答题(8分)',

idProp: 'questionId',

code: 'subjectivity',

},

{

title: '7.判断题(8分)',

idProp: 'questionId',

code: 'judgment',

},

{

title: '8.多选题(8分)',

idProp: 'questionId',

code: 'multiple_choice',

},

],

},

{

title: '二、阅读(40分) (共5题,共40.0分)',

children: [

{

title: '6.复合题(40分)',

idProp: 'questionId',

code: 'complex',

children: [

{

title: '6.(1)填空题(8分)',

idProp: 'questionItemId',

code: 'completion',

blankNum: 4,

showBorder: false,

},

{

title: '6.(2)单选题(8分)',

idProp: 'questionItemId',

code: 'single_choice',

showBorder: false,

},

{

title: '6.(3)问答题(8分)',

idProp: 'questionItemId',

code: 'subjectivity',

},

{

title: '6.(4)多选题(8分)',

idProp: 'questionItemId',

code: 'multiple_choice',

showBorder: false,

},

{

title: '6.(5)判断题(8分)',

idProp: 'questionItemId',

code: 'judgment',

showBorder: false,

},

],

},

],

},

{

title: '三、写作(50分) (共1题,共50.0分)',

children: [

{

title: '7.问答题(50分)',

idProp: 'questionId',

code: 'subjectivity',

},

],

},

];

var sweepPaperTree = transformPaperTree(paperTree);

console.log(sweepPaperTree);

以上是 如何处理这样一个数据? 的全部内容, 来源链接: utcz.com/p/934479.html

回到顶部