javascript - 优化将对象键值推送到数组的循环

标签 javascript arrays

我有一个包含问题的对象,每个问题都有不同的难度,用 allQuiz[x].difficulty 表示。

我写了一个 for 循环,它将根据相对难度类别对问题进行排序。例如难度 25 = 简单,难度 50 = 中等。

这是对象结构的一部分:

 {
    "id": "41",
    "category": "TV & Movies",
    "locales": "GB#US",
    "difficulty": "50",
    "question": {
       "Voice": "Which fictional city is the home of Batman?",
       "Display": "Which fictional city is the home of Batman?"
    },
    "answer": {
       "Voice": "Gotham City",
       "Display": "Gotham City"
    }
 },
 {
    "id": "42",
    "category": "TV & Movies",
    "locales": "GB#US",
    "difficulty": "50",
    "question": {
       "Voice": "The Hunchback of Notre Dame was also known as?",
       "Display": "The Hunchback of Notre Dame was also known as?"
    },
    "answer": {
       "Voice": "Quasimodo",
       "Display": "Quasimodo"
    }
 },
 {
    "id": "43",
    "category": "TV & Movies",
    "locales": "GB#US",
    "difficulty": "75",
    "question": {
       "Voice": "The title role of the 1990 movie Pretty Woman was played by which actress?",
       "Display": "The title role of the 1990 movie Pretty Woman was played by which actress?"
    },
    "answer": {
       "Voice": "Julia Roberts",
       "Display": "Julia Roberts"
    }
 },
 {
    "id": "44",
    "category": "TV & Movies",
    "locales": "GB#US",
    "difficulty": "75",
    "question": {
       "Voice": "Which Tom Hanks movie won the Academy Award for Best Picture in 1994?",
       "Display": "Which Tom Hanks movie won the Academy Award for Best Picture in 1994?"
    },
    "answer": {
       "Voice": "Forrest Gump",
       "Display": "Forrest Gump"
    }
 },
 {
    "id": "45",
    "category": "TV & Movies",
    "locales": "GB#US",
    "difficulty": "100",
    "question": {
       "Voice": "Jonny Depp starred as Jack Sparrow in which series of movies?",
       "Display": "Jonny Depp starred as Jack Sparrow in which series of movies?"
    },
    "answer": {
       "Voice": "Pirates of the Caribbean",
       "Display": "Pirates of the Caribbean"
    }
 },
 {
    "id": "46",
    "category": "TV & Movies",
    "locales": "GB#US",
    "difficulty": "100",
    "question": {
       "Voice": "Marion Crane is the tragic figure in which cult horror film?",
       "Display": "Marion Crane is the tragic figure in which cult horror film?"
    },
    "answer": {
       "Voice": "Psycho",
       "Display": "Psycho"
    }
 },


    {
        "id": "47",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "25",
        "question": {
           "Voice": "Alexander Graham Bell invented which communication tool?",
           "Display": "Alexander Graham Bell invented which communication tool?"
        },
        "answer": {
           "Voice": "Telephone",
           "Display": "Telephone"
        }
     },
 {
        "id": "48",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "75",
        "question": {
           "Voice": "Steve jobs was the founder of which tech giant?",
           "Display": "Steve jobs was the founder of which tech giant?"
        },
        "answer": {
           "Voice": "Apple",
           "Display": "Apple"
        }
     },
     {
        "id": "49",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "75",
        "question": {
           "Voice": "Which year was Amazon founded?",
           "Display": "Which year was Amazon founded?"
        },
        "answer": {
           "Voice": "1994",
           "Display": "1994"
        }
     },
     {
        "id": "50",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "100",
        "question": {
           "Voice": "What is the natural habitat of an arboreal creature?",
           "Display": "What is the natural habitat of an arboreal creature?"
        },
        "answer": {
           "Voice": "Trees",
           "Display": "Trees"
        }
     },
     {
        "id": "51",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "25",
        "question": {
           "Voice": "What is a geiger counter used to detect?",
           "Display": "What is a geiger counter used to detect?"
        },
        "answer": {
           "Voice": "Radiation",
           "Display": "Radiation"
        }
     },
     {
        "id": "52",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "50",
        "question": {
           "Voice": "What element on the periodic table has the chemical symbol H?",
           "Display": "What element on the periodic table has the chemical symbol H?"
        },
        "answer": {
           "Voice": "Hydrogen",
           "Display": "Hydrogen"
        }
     },
     {
        "id": "53",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "50",
        "question": {
           "Voice": "What is the more common name for Nitrous Oxide? �",
           "Display": "What is the more common name for Nitrous Oxide? �"
        },
        "answer": {
           "Voice": "Laughing gas",
           "Display": "Laughing gas"
        }
     },
     {
        "id": "54",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "75",
        "question": {
           "Voice": "In computing, how many bits are in a byte?",
           "Display": "In computing, how many bits are in a byte?"
        },
        "answer": {
           "Voice": "Eight",
           "Display": "Eight"
        }
     },
     {
        "id": "55",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "50",
        "question": {
           "Voice": "Who discovered Penicillin?",
           "Display": "Who discovered Penicillin?"
        },
        "answer": {
           "Voice": "Alexander Fleming",
           "Display": "Alexander Fleming"
        }
     },
     {
        "id": "56",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "25",
        "question": {
           "Voice": "Which planet in our solar system is closest to the sun?",
           "Display": "Which planet in our solar system is closest to the sun?"
        },
        "answer": {
           "Voice": "Mercury",
           "Display": "Mercury"
        }
     },
     {
        "id": "57",
        "category": "Science & Technology",
        "locales": "GB#US",
        "difficulty": "25",
        "question": {
           "Voice": "What subject did Mark Zuckerburg study at university?",
           "Display": "What subject did Mark Zuckerburg study at university?"
        },
        "answer": {
           "Voice": "Computer Science",
           "Display": "Computer Science"
        }
     },

这是我的尝试:

var allQuiz= myObject;

var easyQuiz =[];
var mediumQuiz =[];
var hardQuiz =[];
var veryhardQuiz =[];


for(let x=0;x<allQuiz.length;x++){

    if(allQuiz[x].difficulty==25){
        let tmp = [];
        tmp.push((allQuiz[x]).id);
    tmp.push((allQuiz[x]).question.Display);
    tmp.push((allQuiz[x]).answer.Display);
    easyQuiz.push(tmp);
}
else if(allQuiz[x].difficulty==50){
    let tmp =[];
    tmp.push((allQuiz[x]).id);
    tmp.push((allQuiz[x]).question.Display);
    tmp.push((allQuiz[x]).answer.Display);
    mediumQuiz.push(tmp);

}
else if(allQuiz[x].difficulty==75){
    let tmp =[];
    tmp.push((allQuiz[x]).id);
    tmp.push((allQuiz[x]).question.Display);
    tmp.push((allQuiz[x]).answer.Display);
    hardQuiz.push(tmp);

}
else {
    let tmp = [];
    tmp.push((allQuiz[x]).id);
    tmp.push((allQuiz[x]).question.Display);
    tmp.push((allQuiz[x]).answer.Display);
    veryhardQuiz.push(tmp);
} 
}

但是,我注意到这里有很多代码重复,唯一改变的因素是我推送的测验类别和问题的难度。

我怎样才能使这个 for 循环更优雅或更高效?

最佳答案

我不认为我可以在不牺牲可读性的情况下让它更简洁:

var allQuiz= myObject;
var quizzesByDifficulty = { 25: [], 50: [], 75: [], 100: [] };
for(var x in allQuiz)
{
  var quiz = allQuiz[x];
  var quizzes = quizzesByDifficulty[quiz.difficulty];
  quizzes.push([
    quiz.id,
    quiz.question.Display,
    quiz.answer.Display
  ]);
}

如果你需要每个难度都是它自己的数组,你总是可以这样做:

var easy = quizzesByDifficult[25];
var medium = quizzesByDifficulty[50];
// etc.

编辑:当然,以上假设只有 4 个困难可用。如果这是您无法做出的假设,您仍然可以修改代码来说明这一点:

var allQuiz= myObject;
var quizzesByDifficulty = {};
for(var x in allQuiz)
{
  var quiz = allQuiz[x];

  if (!quizzesByDifficulty[quiz.difficulty]) {
    quizzesByDifficulty[quiz.difficulty] = [];
  }

  var quizzes = quizzesByDifficulty[quiz.difficulty];
  quizzes.push([
    quiz.id,
    quiz.question.Display,
    quiz.answer.Display
  ]);
}

关于javascript - 优化将对象键值推送到数组的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52651945/

相关文章:

javascript - 将 OnClick 属性添加到数组 javascript

javascript - 在其他 float <li> 之前插入(使用 AJAX) float <li> 元素而不移动它们

javascript - $.html() $.load() 中的 &lt;script&gt; 规范是什么?

javascript - 如何从 javascript 中的现有数组创建对象数组?

javascript - 如何将对象转换为按对象键分组

c - 用 C 中的数字对填充数组

c - 在文件作用域中使用C初始化数组大小

javascript - 使用 ReactJS 自动选择 span 标签

javascript - 通过算法将冗余代码优化为循环

java - 有没有办法将泛型参数限制为数组?