将来自 post 请求的数据排序到 json 预定义模式中

sort data from post request into a json predefined schema

我正在尝试根据 post 请求对信息进行“自动”排序。

这是 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) 一起使用,但没有成功。 预先感谢您给我的任何答案!

您可以遍历数据的键,遍历从 0data['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)