将来自 post 请求的数据排序到 json 预定义模式中
sort data from post request into a json predefined schema
我正在尝试根据 post 请求对信息进行“自动”排序。
- 每个类别都由一个数字定义 (0 -> 1 -> ....)
- 每个类别 1 的标题由其编号定义,例如:“0”:“飞机在飞吗?”
- 从 A 到 E 的 5 个问题,其名称中包含值 q 例如:“0-qA”:“可能”
- 每个类别的答案例如:“0-A”:“开”,但如果 0-B 没有被检查,那么将没有任何内容。
这是 req.body 的结果:
{
'0': 'Are planes flying ?',
'1': 'Are tomatoes flying ?',
title: 'What flies',
'0-qA': 'Maybe,
'0-A': 'on'
'0-qB': 'Yes',
'0-qC': 'No',
'0-C': 'on',
'0-qD': 'Ask to Patrick',
'0-D': 'on',
'0-qE': 'Answer C',
'1-qA': 'Maybe no',
'1-qB': 'Yes',
'1-qC': 'No',
'1-C': 'on',
'1-qD': 'Do not ask me',
'1-D': 'on',
'1-qE': 'Answer A',
'1-E': 'on'
'num': '1' //equal to how many questions I have
}
然后我想将它们“排序”为如下所示:
{
"title": "What flies",
"questions": [
{
"question": "Are planes flying ?",
"choix": [
{
"id": "A",
"text": "Maybe"
},
{
"id": "B",
"text": "Yes"
},
{
"id": "C",
"text": "No"
},
{
"id": "D",
"text": "Ask to patrick"
},
{
"id": "E",
"text": "Answer C"
}
],
"answers": [
"0-A",
"0-C",
"0-D"
]
},
{
"question": "Are tomatoes flying ?",
"choix": [
{
"id": "A",
"text": "Maybe no"
},
{
"id": "B",
"text": "Yes"
},
{
"id": "C",
"text": "No"
},
{
"id": "D",
"text": "Don't ask me"
},
{
"id": "E",
"text": "Answer A"
}
],
"answers": [
"1-C",
"1-D",
"1-E"
]
},
]
}
我试过将 num 与 for(i in num)
一起使用,但没有成功。
预先感谢您给我的任何答案!
您可以遍历数据的键,遍历从 0
到 data['num']
的值,并根据键的模式匹配选择和答案(选择以数字开头,然后 -q
并用数字后跟 -
和一个不是 q
:
的字母作为答案
const data = {
'0': 'Are planes flying ?',
'1': 'Are tomatoes flying ?',
'title': 'What flies',
'0-qA': 'Maybe',
'0-A': 'on',
'0-qB': 'Yes',
'0-qC': 'No',
'0-C': 'on',
'0-qD': 'Ask to Patrick',
'0-D': 'on',
'0-qE': 'Answer C',
'1-qA': 'Maybe no',
'1-qB': 'Yes',
'1-qC': 'No',
'1-C': 'on',
'1-qD': 'Do not ask me',
'1-D': 'on',
'1-qE': 'Answer A',
'1-E': 'on',
'num': '1' //equal to how many questions I have
};
let result = {
'title': data['title'],
'questions': []
};
for (let i = 0; i <= parseInt(data['num']); i++) {
let key = i + '';
let question = {
'question': data[key],
'choix': Object.keys(data)
.filter(k => k.startsWith(`${key}-q`))
.map(k => ({
id: k.split('q')[1],
text: data[k]
})),
'answers': Object.keys(data)
.filter(k => k.match(new RegExp(`${key}-[^q]$`)))
};
result['questions'].push(question);
}
console.log(result)
我正在尝试根据 post 请求对信息进行“自动”排序。
- 每个类别都由一个数字定义 (0 -> 1 -> ....)
- 每个类别 1 的标题由其编号定义,例如:“0”:“飞机在飞吗?”
- 从 A 到 E 的 5 个问题,其名称中包含值 q 例如:“0-qA”:“可能”
- 每个类别的答案例如:“0-A”:“开”,但如果 0-B 没有被检查,那么将没有任何内容。
这是 req.body 的结果:
{
'0': 'Are planes flying ?',
'1': 'Are tomatoes flying ?',
title: 'What flies',
'0-qA': 'Maybe,
'0-A': 'on'
'0-qB': 'Yes',
'0-qC': 'No',
'0-C': 'on',
'0-qD': 'Ask to Patrick',
'0-D': 'on',
'0-qE': 'Answer C',
'1-qA': 'Maybe no',
'1-qB': 'Yes',
'1-qC': 'No',
'1-C': 'on',
'1-qD': 'Do not ask me',
'1-D': 'on',
'1-qE': 'Answer A',
'1-E': 'on'
'num': '1' //equal to how many questions I have
}
然后我想将它们“排序”为如下所示:
{
"title": "What flies",
"questions": [
{
"question": "Are planes flying ?",
"choix": [
{
"id": "A",
"text": "Maybe"
},
{
"id": "B",
"text": "Yes"
},
{
"id": "C",
"text": "No"
},
{
"id": "D",
"text": "Ask to patrick"
},
{
"id": "E",
"text": "Answer C"
}
],
"answers": [
"0-A",
"0-C",
"0-D"
]
},
{
"question": "Are tomatoes flying ?",
"choix": [
{
"id": "A",
"text": "Maybe no"
},
{
"id": "B",
"text": "Yes"
},
{
"id": "C",
"text": "No"
},
{
"id": "D",
"text": "Don't ask me"
},
{
"id": "E",
"text": "Answer A"
}
],
"answers": [
"1-C",
"1-D",
"1-E"
]
},
]
}
我试过将 num 与 for(i in num)
一起使用,但没有成功。
预先感谢您给我的任何答案!
您可以遍历数据的键,遍历从 0
到 data['num']
的值,并根据键的模式匹配选择和答案(选择以数字开头,然后 -q
并用数字后跟 -
和一个不是 q
:
const data = {
'0': 'Are planes flying ?',
'1': 'Are tomatoes flying ?',
'title': 'What flies',
'0-qA': 'Maybe',
'0-A': 'on',
'0-qB': 'Yes',
'0-qC': 'No',
'0-C': 'on',
'0-qD': 'Ask to Patrick',
'0-D': 'on',
'0-qE': 'Answer C',
'1-qA': 'Maybe no',
'1-qB': 'Yes',
'1-qC': 'No',
'1-C': 'on',
'1-qD': 'Do not ask me',
'1-D': 'on',
'1-qE': 'Answer A',
'1-E': 'on',
'num': '1' //equal to how many questions I have
};
let result = {
'title': data['title'],
'questions': []
};
for (let i = 0; i <= parseInt(data['num']); i++) {
let key = i + '';
let question = {
'question': data[key],
'choix': Object.keys(data)
.filter(k => k.startsWith(`${key}-q`))
.map(k => ({
id: k.split('q')[1],
text: data[k]
})),
'answers': Object.keys(data)
.filter(k => k.match(new RegExp(`${key}-[^q]$`)))
};
result['questions'].push(question);
}
console.log(result)