重新格式化JSON

cclgggtu  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(193)

我要把它用在:

[
   {
      "questionId":1,
      "level":1,
      "difficultyLevel":1.1,
      "stage":1,
      "item #":1,
      "targetWord":"example1",
      "category":"hard"
   },
   {
      "questionId":2,
      "level":1,
      "difficultyLevel":1.1,
      "stage":1,
      "item #":2,
      "targetWord":"example2",
      "category":"hard"
   },
   {
      "questionId":3,
      "level":1,
      "difficultyLeveluest":1.1,
      "stage":1,
      "item #":3,
      "targetWord":"example3",
      "category":"soft"
   },
   {
      "questionId":4,
      "level":1,
      "difficultyLevel":"1.1b",
      "stage":1,
      "item #":4,
      "targetWord":"example4",
      "category":"hard"
   },
   {
      "questionId":5,
      "level":1,
      "difficultyLevel":"1.1b",
      "stage":1,
      "item #":5,
      "targetWord":"example5",
      "category":"soft"
   },
   {
      "questionId":6,
      "level":1,
      "difficultyLevel":"1.1b",
      "stage":1,
      "item #":6,
      "targetWord":"example6",
      "category":"hard"
   },
   {
      "questionId":7,
      "level":1,
      "difficultyLevel":1.2,
      "stage":1,
      "item #":7,
      "targetWord":"example7",
      "category":"hard"
   },
   {
      "questionId":8,
      "level":1,
      "difficultyLevel":1.2,
      "stage":1,
      "item #":8,
      "targetWord":"example8",
      "category":"soft"
   },
   {
      "questionId":9,
      "level":1,
      "difficultyLevel":1.2,
      "stage":1,
      "item #":9,
      "targetWord":"example9",
      "category":"soft"
   },
   {
      "questionId":10,
      "level":1,
      "difficultyLevel":"1.2b",
      "stage":1,
      "item #":1,
      "targetWord":"example10",
      "category":"hard"
   },
   {
      "questionId":11,
      "level":1,
      "difficultyLevel":"1.2b",
      "stage":1,
      "item #":2,
      "targetWord":"example11",
      "category":"hard"
   },
   {
      "questionId":12,
      "level":1,
      "difficultyLevel":"1.2b",
      "stage":1,
      "item #":3,
      "targetWord":"example12",
      "category":"soft"
   }
]

我需要重新格式化它,并按级别将其分开,我认为它看起来像这样应该是有意义的:

levels{
  1{
    difficulty{
      1.1{
        questions[
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
         ]
       }
      1.1b{
        questions[
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
         ]
       }
     }
  }
  2{
    difficulty{
      1.2{
        questions[
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
         ]
       }
      1.2b{
        questions[
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
           {
             questionId
             targetWord
             item
           }
         ]
       }
     }
  }
}

但我真的不知道该怎么做......以下是我能做的:

{
  levels: {
    '1': {
      dificulty: '1.1b'
      questionId: 6,
      targetWord: 'example6',
    },
    '2': {
      dificulty: '1.2b'
      questionId: 12,
      targetWord: 'example12',
    }
  }
}

上面的代码片段是通过以下方式实现的:

const setLevels = (levelMapData) => {
  const levelMap = {};

  levelMapData
    .forEach(({ level, questionId, targetWord, dificultyLevel }) => {
      levelMap[level] = {
        dificultyLevel,
        questionId, 
        targetWord,
      };
    });

  return levelMap;
}

module.exports = async ([levelMapData]) => {
  const levels = setLevels(levelMapData);

  const curriculum = {
    levels,
  };

  return curriculum;
};

我怎样才能使它的格式正确?请帮助。并提前非常感谢你。

cedebl8k

cedebl8k1#

我们需要对difficultyLevel重复对level所做的操作,但要在目标数据结构中更深的层次上进行,可以使用??=操作符创建所需但还不存在的属性,然后向下钻取到必须执行push以添加问题对象的位置:

function tree(levelMapData) {
     const levelMap = {};
     for (const { level, difficultyLevel, questionId, targetWord, "item #": item } of levelMapData) {
         ((levelMap[level] ??= {})[difficultyLevel] ??= []).push({
             questionId, targetWord, item 
         });
     }
     return levelMap;
}

const levelMapData = [{"questionId":1,"level":1,"difficultyLevel":1.1,"stage":1,"item #":1,"targetWord":"example1","category":"hard"},{"questionId":2,"level":1,"difficultyLevel":1.1,"stage":1,"item #":2,"targetWord":"example2","category":"hard"},{"questionId":3,"level":1,"difficultyLeveluest":1.1,"stage":1,"item #":3,"targetWord":"example3","category":"soft"},{"questionId":4,"level":1,"difficultyLevel":"1.1b","stage":1,"item #":4,"targetWord":"example4","category":"hard"},{"questionId":5,"level":1,"difficultyLevel":"1.1b","stage":1,"item #":5,"targetWord":"example5","category":"soft"},{"questionId":6,"level":1,"difficultyLevel":"1.1b","stage":1,"item #":6,"targetWord":"example6","category":"hard"},{"questionId":7,"level":1,"difficultyLevel":1.2,"stage":1,"item #":7,"targetWord":"example7","category":"hard"},{"questionId":8,"level":1,"difficultyLevel":1.2,"stage":1,"item #":8,"targetWord":"example8","category":"soft"},{"questionId":9,"level":1,"difficultyLevel":1.2,"stage":1,"item #":9,"targetWord":"example9","category":"soft"},{"questionId":10,"level":1,"difficultyLevel":"1.2b","stage":1,"item #":1,"targetWord":"example10","category":"hard"},{"questionId":11,"level":1,"difficultyLevel":"1.2b","stage":1,"item #":2,"targetWord":"example11","category":"hard"},{"questionId":12,"level":1,"difficultyLevel":"1.2b","stage":1,"item #":3,"targetWord":"example12","category":"soft"}];

const result = tree(levelMapData);
console.log(result);

相关问题