Javascript 优化将对象键值推送到数组的循环
我有一个包含问题的对象,每个问题都有不同的难度,用Javascript 优化将对象键值推送到数组的循环,javascript,arrays,Javascript,Arrays,我有一个包含问题的对象,每个问题都有不同的难度,用allquick[x]表示。难度 我已经为循环编写了一个,它将根据问题的相对难度类别对问题进行排序。例如难度25=容易,难度50=中等等等 以下是对象结构的一部分: { "id": "41", "category": "TV & Movies", "locales": "GB#US", "difficulty": "50", "question": { "Voice": "Which
allquick[x]表示。难度
我已经为循环编写了一个,它将根据问题的相对难度类别对问题进行排序。例如难度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);
}
}
var allquick=myObject;
var EasyQuit=[];
var mediumquick=[];
var hardquick=[];
var veryhardquick=[];
for(让x=0;x数组。reduce
对于此类数据转换任务来说是一个方便的函数。其思想是创建一个对象,该对象由映射到一个值数组的难度决定。最后,生成的对象可以拆分为您所需的四个结构或按原样使用(如果您愿意,可以使用从难度数字到难度单词的键映射)
var allquick=[{“id”:“41”,“category”:“TV&Movies”,“locales”:“GB#US”,“难度”:“50”,“问题”:{“Voice”:“哪个虚拟城市是蝙蝠侠的家?”,“Display”:“哪个虚拟城市是蝙蝠侠的家?”,”回答:{“Voice”:“Gotham city”,“Display”:“Gotham city”},{“id”:“42”,“category”:“TV&Movies”,“locales”问题:{“声音”:“圣母院的驼背也被称为?”,“展示”:“圣母院的驼背也被称为?”,“回答:{“声音”:“卡西莫多”,“展示”:“卡西莫多”},{“id”:“43”,“类别”:“电视和电影”,“地点”:“英国”;“美国”;“难度”:“75”,“问题”:{“声音”:“1990年电影《美丽女人》的标题角色由哪位女演员扮演?”,“展示”:“1990年电影《美丽女人》的标题角色由哪位女演员扮演?”,“回答:{“声音”:“朱莉娅·罗伯茨”,“展示”:“朱莉娅·罗伯茨”}},{“id”:“44”,“类别”:“电视与电影”,“地区”:“GB#US”,“难度”:“75”,“问题”:{“声音”:”汤姆·汉克斯的哪部电影获得了1994年奥斯卡最佳影片奖?,“展示”:“汤姆·汉克斯的哪部电影获得了1994年奥斯卡最佳影片奖?”,“回答”:{“声音”:“阿甘”,“展示”:“阿甘”},{“id”:“45”,“类别”:“电视和电影”,“地区”:“美国”,“难度”:“100”,“问题”:{“声音”:”乔尼·德普在哪部电影系列中饰演杰克·斯派洛?,“展示”:“乔尼·德普在哪部电影系列中饰演杰克·斯派洛?”,“回答”:{“声音”:“加勒比海盗”,“展示”:“加勒比海盗”},{“id”:“46”,“类别”:“电视和电影”,“地区”:“美国”,“困难”:“100”,“问题”:{“声音”:”Marion Crane是哪个邪教恐怖片中的悲剧人物?,“展示”:“Marion Crane是哪个邪教恐怖片中的悲剧人物?”,“回答”:{“声音”:“心理”,“展示”:“心理”},{“id”:“47”,“类别”:“科学技术”,“地点”:“美国”,“困难”:“25”,“问题”:{“声音”:”亚历山大·格雷厄姆·贝尔发明了哪种通信工具?,“显示”:“亚历山大·格雷厄姆·贝尔发明了哪种通信工具?”,“回答”:{“声音”:“电话”,“显示”:“电话”},{“id”:“48”,“类别”:“科学技术”,“地区”:“美国”,“困难”:“75”,“问题”:{“声音”:史蒂夫·乔布斯是哪家科技巨头的创始人?,“显示器”:“史蒂夫·乔布斯是哪家科技巨头的创始人?”,“回答”:{“声音”:“苹果”,“显示器”:“苹果”},{“id”:“49”,“类别”:“科学技术”,“地区”:“美国”,“困难”:“75”,“问题”:{“声音”:“亚马逊成立于哪一年?”,“显示器”:”亚马逊成立于哪一年?},“回答:{“声音”:“1994”,“展示”:“1994”},{“id”:“50”,“类别”:“科学与技术”,“地点”:“GB#US”,“难度”:“100”,“问题”:{“声音”:“树栖生物的自然栖息地是什么?”,“展示”:“树栖生物的自然栖息地是什么?”},“回答:{“声音”:”“树”,“显示”:“树”},{“id”:“51”,“类别”:“科学与技术”,“地区”:“GB#US”,“难度”:“25”,“问题”:{“声音”:“盖革计数器用于检测什么?”,“显示”:“盖革计数器用于检测什么?”},{“声音”:“辐射”,“显示”:“辐射”},{“id”:“52”,“类别”:”科学与技术,“地区”:“GB#US”,“难度”:“50”,“问题”:{“声音”:“周期表上的什么元素有化学符号H?”,“显示”:“周期表上的什么元素有化学符号H?”,“回答”:{“声音”:“氢”,“显示”:“氢”},{“id”:“53”,“类别”:“科学与技术”,“地区”“:“GB#US”,“难度”:“50”,“问题”:{“声音”:“氧化亚氮更常见的名称是什么?”?�", "显示“:“一氧化二氮更常见的名称是什么?�" }, "答案:{“语音”:“笑气”,“显示”:“笑气”},{“id”:“54”,“类别”:“科学与技术”,“地区”:“GB#US”,“难度”:“75”,“问题”:{“语音”:“在计算中,一个字节有多少位?”,“显示”:“在计算中,一个字节有多少位?”},答案:{“语音”:“八”,“显示”:“八”},{“id”:“55”,“类别”:“科学与技术”,“地点”:“GB#US”,“难度”:“50”,“问题”:{“声音”:“谁发现了青霉素?”,“显示”:“谁发现了青霉素?”,“回答”:{“声音”:“亚历山大·弗莱明”,“显示”:“亚历山大·弗莱明”},{“id”:“56”,“类别”:“科学与技术”,“地点”:“GB#US”,“难度”:“25”问题:{“声音”:“我们太阳系中哪颗行星离太阳最近?”,“显示”:“我们太阳系中哪颗行星离太阳最近?”,“回答:{“声音”:“水星”,“显示”:“水星”}},{“id”:“57”,“类别”:“科学技术”,“地点”:“美国”,“困难”:“25”,“问题”:“声音”:”马克·扎克伯格在大学学习了什么科目?,“显示”:“马克·扎克伯格在大学学习了什么科目?”,“回答:{“声音”:“计算机科学”,“显示”:“计算机科学”
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.
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
]);
}