首页 > 解决方案 > 如何根据标准将一组对象映射到另一个对象?

问题描述

如何根据标准将一组对象映射到另一个对象?

如何将以下问题数组转换为预期数组?



const questions = [
                { type: 'question', index: 1 },
                { type: 'question', index: 2 },
                { type: 'answer', index: 3 },
                { type: 'answer', index: 4 },
                { type: 'answer', index: 5 },
                { type: 'answer', index: 6 },
                { type: 'answer', index: 7 },
                { type: 'answer', index: 8 },
                { type: 'answer', index: 9 },
                { type: 'question', index: 11 },
                { type: 'answer', index: 12 },
                { type: 'answer', index: 13 },
                { type: 'question', index: 14 },
                { type: 'question', index: 15 },
                { type: 'question', index: 16 },
                { type: 'question', index: 17 },
                { type: 'answer', index: 18 },
                { type: 'answer', index: 19 },
                { type: 'question', index: 20 },
                { type: 'question', index: 21 },
                { type: 'question', index: 22 },
                { type: 'question', index: 23 },
                { type: 'question', index: 24 },
                { type: 'question', index: 25 },
                { type: 'question', index: 26 },
                { type: 'question', index: 27 },
                { type: 'question', index: 28 },
                { type: 'question', index: 100 },
                {}
          ];
              

这是预期的结果数组。

                    const expected = [
                      { type: "question", index: 1, answers: [] },
                      {
                        type: "question",
                        index: 2,
                        answers: [
                          { type: "answer", index: 3 },
                          { type: "answer", index: 4 },
                          { type: "answer", index: 5 },
                          { type: "answer", index: 6 },
                          { type: "answer", index: 7 },
                          { type: "answer", index: 8 },
                          { type: "answer", index: 9 },
                        ],
                      },

                      {
                        type: "question",
                        index: 11,
                        answers: [
                          { type: "answer", index: 12 },
                          { type: "answer", index: 13 },
                        ],
                      },

                      { type: "question", index: 14, answers: [] },
                      { type: "question", index: 15, answers: [] },
                      { type: "question", index: 16, answers: [] },
                      {
                        type: "question",
                        index: 17,
                        answers: [
                          { type: "answer", index: 18 },
                          { type: "answer", index: 19 },
                        ],
                      },

                      { type: "question", index: 20, answers: []},
                      { type: "question", index: 21, answers: []},
                      { type: "question", index: 22, answers: []},
                      { type: "question", index: 23, answers: []},
                      { type: "question", index: 24, answers: []},
                      { type: "question", index: 25, answers: []},
                      { type: "question", index: 26, answers: []},
                      { type: "question", index: 27, answers: []},
                      { type: "question", index: 28, answers: []},
                      { type: 'question', index: 100, answers: [] },
                      {}
                    ];

所以想法是调用一个子数组answers,我们用type === 'answer'.

所以答案应该被推送到索引n-1type === 'question'直到type === 'question'遇到下一个具有类型的项目。如果中间没有答案,则保持数组为空。

基本上我们寻找索引和类型

更新

所以我尝试的方法是创建一个包含索引的数组。每次循环时,我都会寻找下一个最大索引及其类型。后来基于此,我尝试将中间索引复制到前一个索引。

标签: javascript

解决方案


我想你可以reduce在这里使用。这就是我的想法。

const questions = []; // your questions here

questions.reduce((acc, question) => {
  if (question.type === 'question') return [...acc, Object.assign({}, question, { answers: [] })];
  else if (question.type === 'answer') {
    return [
      ...acc.slice(0, acc.length-1), 
      Object.assign({}, acc[acc.length-1], {
        answers: [...acc[acc.length-1].answers, question]
      })
    ];
  }
}, []);

好吧,代码看起来不漂亮而且不可读。questions如果可以使代码更漂亮,我建议更改您的数据结构。


推荐阅读