javascript 将键添加为路径的一部分

mo49yndu  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(127)

我想在path中使用i来获取json中的值

export const getLevelLetters = () => {
  const levelLetters = [];
  for (let i = 0; i < 3; i++) {
    levelLetters.push([]);
    levelLetters[i].push(curriculum.levels.i[0].grapheme);
    levelLetters[i].push(curriculum.levels.i[1].grapheme);
    levelLetters[i].push(curriculum.levels.i[2].grapheme);
    levelLetters[i].push(curriculum.levels.i[3].grapheme);
  }
  return levelLetters;
};

json示例:

{
  "version": "1.0.0",
  "levels": {
    "1": [
      { "questionId": 1, "grapheme": "a" },
      { "questionId": 2, "grapheme": "o" },
      { "questionId": 3, "grapheme": "i" },
      { "questionId": 4, "grapheme": "e" },
    ],
    "2": [
      { "questionId": 1, "grapheme": "i" },
      { "questionId": 2, "grapheme": "o" },
      { "questionId": 6, "grapheme": "m" },
      { "questionId": 7, "grapheme": "p" }
    ],
  }
}

我如何在courmine.levels.i[0]中使用1、2或其他值?
我试着使成一个变量:
考试= i ....和正在进行的课程.水平.考试[0]
没起作用。
我知道这可能是一个简单的问题和一个简单的解决方案,但我就是看不到它。请帮助。

n53p2ov0

n53p2ov01#

尝试一下,你就会知道你的代码出了什么问题。

const getLevelLetters = () => {
  const levelLetters = [];
  for (let i = 0; i < 2; i++) {
    levelLetters.push([]);
    levelLetters[i].push(curriculum.levels[i + 1][0].grapheme);
    levelLetters[i].push(curriculum.levels[i + 1][1].grapheme);
    levelLetters[i].push(curriculum.levels[i + 1][2].grapheme);
    levelLetters[i].push(curriculum.levels[i + 1][3].grapheme);
  }
  return levelLetters;
};

相关问题