简体   繁体   English

使用JavaScript在函数内部创建新对象?

[英]Create new objects inside of a function with JavaScript?

So I want to create a function that if, if a question is answered wrong, it stores the wrong answer into a new object? 因此,我想创建一个函数,如果一个问题的回答错误,它将错误的答案存储到一个新对象中? So that I can list all of the wrong answers at the end? 这样我可以在最后列出所有错误答案?

var answerList = [{question:1, answer:1},{question:2, answer:2},]
var listofWrongAnswers = [];

if (answerSelected != answerlist[1].answer) {
    /* create a new object
       put answerlist[1].question and answerlist[i].answer in new object and push to 
       array ListofWrongAnswers */
}

I dont get how you can randomly name variables? 我不知道如何随机命名变量? if that's even possible. 如果可能的话。

Couldn't you simply create a new object based on the current question? 您不能根据当前问题简单地创建一个新对象吗?

Something like this should work: 这样的事情应该起作用:

if (answerSelected != answerlist[1].answer) {

    listofWrongAnswers.push({question: answerlist[1].question, answer: answerlist[1].answer});
}

However, you should look to make that index a parameter: 但是,您应该使该索引成为参数:

function addToWrongAnswers(answerSelected, idx) {

    if (answerSelected != answerlist[idx].answer) {

        listofWrongAnswers.push({question: answerlist[idx].question, answer: answerlist[idx].answer});
    }
}

I assume you want something like this: 我假设您想要这样的东西:

function getQuestion(number){
    for(var i = 0; i < answerList.length; i++){
        if(answerList[i].question === number){
            return answerList[i];
        }
    }
}

function checkAnswer(questionNumber, answer){
    var question = getQuestion(questionNumber);
    if(answer !== question.answer){
        listofWrongAnswers[question];
    }
}

However , I'm also assuming you'll start with Question 1 and increment the number, so it would be even simpler if you just selected the question using an array index, rather than iterating through it. 但是 ,我还假设您将从问题1开始并增加数字,因此,如果您仅使用数组索引而不是遍历该问题来选择问题,它将更加简单。 So this might be better: 所以这可能更好:

var answerList = [{question:1, answer:1}, {question:2, answer:2}];
var listofWrongAnswers = [];

function checkAnswer(questionNumber, answer){
    var question = answerList[questionNumber - 1];
    if(answer !== question.answer){
        listofWrongAnswers[question];
    }
}

If you want to clone that question object and push it into that array, you can use 如果要克隆该问题对象并将其推入该数组,则可以使用

listofWrongAnswer.push({question: answerlist[1].question, answer: answerlist[1].answer});

But with this code you won't can use == operator to compare question objects existing at those arrays 但是使用此代码,您将无法使用==运算符比较那些数组中存在的问题对象

listofWrongAnswer[1] == answerlist[1] // It's false

But if you don't want such a behavior you can store only references to question objects in the wrong answer list: 但是,如果您不希望出现这种情况,则只能在错误的答案列表中存储对问题对象的引用:

listofWrongAnswer.push(answerlist[1]);

With this code you can compare question objects with == operator 使用此代码,您可以使用==运算符比较问题对象

listofWrongAnswer[1] == answerlist[1] // It's true

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

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