最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 JavaScript 中循環(huán)構(gòu)造函數(shù)

在 JavaScript 中循環(huán)構(gòu)造函數(shù)

慕萊塢森 2023-11-12 14:28:24
我正在整理一個在線測驗(yàn),并想提出許多問題。我創(chuàng)建了一個類構(gòu)造函數(shù)(見下文)。我已經(jīng)設(shè)置了輸入變量,每個問題的變量都不同。我想迭代它們的構(gòu)造,但我不知道如何循環(huán)并增加我傳遞的對象名稱(例如,q1、q2 等)和參數(shù)(例如,answer0、answer1 等)構(gòu)造函數(shù)。任何幫助將不勝感激!我認(rèn)為如果你看看下面的代碼就很有意義了。我知道一定有更有效的方法。let quest = []; //array of question objects//question constructorclass Question {  constructor(question, answer, hint, icon, congrats, image, location) {    this.answer = answer;    this.congrats = congrats;    this.hint = hint;    this.icon = icon;    this.image = image;    this.location = location;    this.question = question;    this.pushToQuest = function () {      quest.push(this);    };    this.pushToQuest();  }}// Question 0 input (actual text removed)let question0 = "?";let answer0 = ["", "", "", "", ""];let hint0 = ["", "", "", "", ""];let icon0 = "fa-utensils-alt";let image0 = "img/001.jpg";let congrats0 = "That's right.... ";let location0 = '';const q0 = new Question(  question0,  answer0,  hint0,  icon0,  congrats0,  image0,  location0);
查看完整描述

1 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個贊

一個可能的解決方案是創(chuàng)建一個包含問題對象列表的 json 文件,如下所示


[

    {

         "question": "some question",

         "answer": ["", "", "", "", ""];

         "hint": ["", "", "", "", ""];

         "icon": "fa-utensils-alt";

         "image": "img/001.jpg";

         "congrats": "That's right.... ";

         "location": "";

    },

    {

         "question": "some question",

         "answer": ["", "", "", "", ""];

         "hint": ["", "", "", "", ""];

         "icon": "fa-utensils-alt";

         "image": "img/001.jpg";

         "congrats": "That's right.... ";

         "location": "";

    }


]

然后獲取數(shù)據(jù),并使用 foreach 循環(huán)創(chuàng)建一個構(gòu)造函數(shù)并將每個構(gòu)造函數(shù)附加到任務(wù)列表中


async function (fetchedJson) {

    fetchedJson.forEach((item) => {

        const que = new Question(

            item.question,

            item.answer,

            item.hint,

            item.icon,

            item.image,

            item.congrats,

            item.location

        )


        quest.push(que)

    })

}

如果由于我使用平板電腦打字而出現(xiàn)語法錯誤,請?jiān)徫业拇a


查看完整回答
反對 回復(fù) 2023-11-12
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號