第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

慕萊塢森 2023-11-12 14:28:24
我正在整理一個在線測驗,并想提出許多問題。我創(chuàng)建了一個類構造函數(shù)(見下文)。我已經(jīng)設置了輸入變量,每個問題的變量都不同。我想迭代它們的構造,但我不知道如何循環(huán)并增加我傳遞的對象名稱(例如,q1、q2 等)和參數(shù)(例如,answer0、answer1 等)構造函數(shù)。任何幫助將不勝感激!我認為如果你看看下面的代碼就很有意義了。我知道一定有更有效的方法。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貢獻2011條經(jīng)驗 獲得超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)建一個構造函數(shù)并將每個構造函數(shù)附加到任務列表中


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)語法錯誤,請原諒我的代碼


查看完整回答
反對 回復 2023-11-12
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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