简体   繁体   中英

Putting an object into an array in JavaScript

I'm currently working with an MVC JS framework and I want to be able to get a list of objects that I can take a random entry out of on a loop. So far I've managed to create a function that finds a random ID and pulls out that object so that part is not a problem. It's what is going into the array of objects:

QuestionsSetup: function(gameType) {

        // Setup Resources
        var c = this.View.children;
        var player1qs = [];
        var leftQ = 0;
        var rightQ = 0;
        var maxQValue = 50;
        var minQValue = 1;

        // Fill array with questions 
        for (var i = 0; i < 5; i++) {

            // Build a random question with numbers between 1 and 50

            // Build Question Text to output to user

            // Generate correct answers based on generated question

            // Generate unsorted, incorrect answers and add them to an array

            //Place Questions into object
            questions.qId = i;
            questions.leftQ = leftQ;
            questions.rightQ = rightQ;
            questions.correctAnswer = correctAnswer;
            questions.allAnswers = sortedAnswers;
            questions.questionText = questionText;

            //Add to array of questions
            player1qs.push(questions);
        }
    }

This does add them to an array but when adding a new object it also changes the values of the existing objects in the array so they all come out the same no matter which one I pull out later. The questions object is declared in it's own file in a models folder. Is there any way, at the start of each loop, to tell the application I want new empty questions object as opposed to referencing the existing ones? I know that you can in similar back end languguages so I refuse to beleive that something so simple doesn't exist in JavaScript too?

Declaring a variable for each array item is definitely missing.

QuestionsSetup: function(gameType) {

    // Setup Resources
    var c = this.View.children;
    var player1qs = [];
    var leftQ = 0;
    var rightQ = 0;
    var maxQValue = 50;
    var minQValue = 1;

    // Fill array with questions 
    for (var i = 0; i < 5; i++) {

        var tempQuestion = {
           qId: i,
           leftQ: leftQ,
           rightQ: rightQ,
           correctAnswer: correctAnswer,
           allAnswers: sortedAnswers,
           questionText: questionText
        }


        // ...


        //Add to array of questions
        player1qs.push(tempQuestion);
    }
}

Using a separate closure inside a loop also might be a good idea.

do this:

for (var i = 0; i < 5; i++) {
   let questions = {};
   // the rest....

you need to define the object first.

Maybe you should just initialize the questions object before initializing its properties, so the code should look like this:

       //Place Questions into object
        questions = {};

        questions.qId = i;
        questions.leftQ = leftQ;
        questions.rightQ = rightQ;
        questions.correctAnswer = correctAnswer;
        questions.allAnswers = sortedAnswers;
        questions.questionText = questionText;

        //Add to array of questions
        player1qs.push(questions);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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