简体   繁体   English

显示数组中的下一个对象值

[英]Display next object value in array

I have an array of two objects. 我有两个对象的数组。 When the user presses a button, I would like for the next value of a specific object property to be displayed. 当用户按下按钮时,我希望显示特定对象属性的下一个值。

Here is my array: 这是我的数组:

var allQuestions = [{
    question: "This is question number one",
    choices: ["one", "two", "three", "four"],
    correctAnswer: "two"
}, {
    question: "This is question number two",
    choices: ["dog", "cat", "bear", "lion"],
    correctAnswer: "bear"
}];

When the button is pressed, I would like for the next instance of "question" to be displayed. 当按下按钮时,我希望显示下一个“问题”实例。

Here is my function to switch out the question: 这是我关闭问题的功能:

function switchQuestion() {

    var singleQuestion = 0;

    if(singleQuestion >= allQuestions.length) {
        singleQuestion == 0;
    } else {
        singleQuestion == ""; // not sure what should go here
    }

    document.getElementById('question').innerHTML = allQuestions[singleQuestion].question;

}

You need to scope the question index outside of the function, increment in each time the button is clicked and re-assign it back to 0 when it's outside the bounds of the array: 您需要将问题索引的作用域设置在函数之外,每次单击按钮时将其递增,并在数组边界之外将其重新分配为0:

var questionIndex = 0;
function switchQuestion() {
  if(++questionIndex >= allQuestions.length) {
    questionIndex = 0;
  }

  document.getElementById('question').innerHTML = allQuestions[singleQuestion].question;
}

In this code: 在此代码中:

if(singleQuestion >= allQuestions.length) {
        singleQuestion == 0;
    } else {
        singleQuestion == ""; // not sure what should go here
    }

An assignment is done with = instead of == : 使用=而不是==进行分配:

if (singleQuestion >= allQuestions.length) {
    singleQuestion = 0;
} else {
    singleQuestion = singleQuestion + 1; // increment
}

The increment can also be achieved in this short form: 增量也可以通过以下简短形式实现:

singleQuestion++;

The whole expression can also be replaced by using modulus calculation: 整个表达式也可以通过使用模数计算来代替:

singleQuestion = (singleQuestion + 1) % allQuestions.length;

Lastly, the variable singleQuestion must be defined outside of your function. 最后,必须在函数之外定义变量singleQuestion。

You need to store the currentQuestion somewhere then increment it onclick 您需要将currentQuestion存储在某个位置,然后在单击时增加它

  var singleQuestion = 0;

  function switchQuestion() {

  if(singleQuestion >= allQuestions.length) {
      singleQuestion == 0;
   } else {
    singleQuestion +=1; 
   }

document.getElementById('question').innerHTML = allQuestions[singleQuestion].question;

 }

At present you would reset it back to 0 on every click regardless and only ever show the first or second question based on the length 目前,您每次点击都会将其重置为0,无论如何,仅会根据时长显示第一个或第二个问题

Here is a JSFiddle example that shows possible implementation of your script. 这是一个JSFiddle示例,它显示了脚本的可能实现。

I would suggest using just one global object. 我建议只使用一个全局对象。
And using .createElement() instead of .innerHTML() . 并使用.createElement()而不是.innerHTML() Here is a discussion . 这是一个讨论

In short: 简而言之:

var myGlobalVar = {
    singleQuestion: 0,
    nextButton: document.getElementById("nextQstBtn"),
    questionHolder: document.getElementById("questionHolder"),
    allQuestions: [qstObjOne, qstObjTwo, qstObjThree],

    switchQuestion: function () {
        myGlobalVar.singleQuestion += 1;
        if (myGlobalVar.singleQuestion === myGlobalVar.allQuestions.length) {
                myGlobalVar.singleQuestion = 0;
        }
        myGlobalVar.showQuestion(myGlobalVar.singleQuestion);
    },
    showQuestion: function (qstNum) {
        // Implementation
    },
    init: function () {
        // Script initialisation
        // Attaching events, etc.
};

myGlobalVar.init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM