简体   繁体   English

如何在JavaScript中将对象属性添加到数组中?

[英]How do I add object properties into an array in JavaScript?

Here's a fiddle. 这是一个小提琴。 http://fiddle.jshell.net/GF9nj/1/ What I'm trying to figure out is why when I .push my object data into the randomAnswerArray it doesn't show up even in the testing <p>. http://fiddle.jshell.net/GF9nj/1/我要弄清楚的是为什么当我将对象数据推入randomAnswerArray时,即使在测试<p>中也没有显示。 It's about halfway down the JavaScript section. 它大约在JavaScript部分的中间。

Here is the code in question: 这是有问题的代码:

// incorrectAnswer1, incorrectAnswer2, incorrectAnswer3 are initialised elsewhere ...
randomAnswerArray = []; //resets the array to empty
randomAnswerArray.push(randomQuestion.correctAnswer);
document.getElementById('test3').innerHTML=randomAnswerArray[0]; //TESTING doesn't work
randomAnswerArray.push(randomQuestion.incorrectAnswer1);
randomAnswerArray.push(randomQuestion.incorrectanswer2);
randomAnswerArray.push(randomQuestion.incorrectanswer3);
document.getElementById('test1').innerHTML = randomAnswerArray.valueOf();

the valueOf() call displays only the 0 and 1 items from randomAnswerArray, but I'm pushing 2 & 3 as well. valueOf()调用仅显示randomAnswerArray中的0和1项目,但我也同时推2和3。


Here is the full code of the fiddle: 这是小提琴的完整代码:

HTML HTML

var questionList = [];
var randomAnswerArray = [];

// this is the question object constructor

function quizQuestion(question, correctAnswer, incorrectAnswer1, incorrectAnswer2, incorrectAnswer3) {
    this.question = question;
    this.correctAnswer = correctAnswer;
    this.incorrectAnswer1 = incorrectAnswer1;
    this.incorrectAnswer2 = incorrectAnswer2;
    this.incorrectAnswer3 = incorrectAnswer3;
}

// this constructs a question

var hairColor = new quizQuestion("What color is my hair?", "black", "blue", "red", "purple");

// this adds the question to the questionList

questionList.push(hairColor);
document.getElementById('test2').innerHTML = questionList[0].correctAnswer; //TESTING object constructor works
function generate() {

    // this part picks a random question
    var randomQuestion = questionList[Math.floor(Math.random() * questionList.length)];

    // this part puts the question in the questionString
    document.getElementById("questionString").innerHTML = randomQuestion.question;

    // this part puts the answers in the array

    // THIS IS WHAT WE ARE TESTING    VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
    randomAnswerArray = []; //resets the array to empty
    randomAnswerArray.push(randomQuestion.correctAnswer);
    document.getElementById('test3').innerHTML=randomAnswerArray[0]; //TESTING doesn't work
    randomAnswerArray.push(randomQuestion.incorrectAnswer1);
    randomAnswerArray.push(randomQuestion.incorrectanswer2);
    randomAnswerArray.push(randomQuestion.incorrectanswer3);
    document.getElementById('test1').innerHTML = randomAnswerArray.valueOf(); //TESTING doesn't work
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    
    // this part randomizes the array

    var currentIndex = randomAnswerArray.length;
    var temporaryValue;
    var randomIndex;
    while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = randomAnswerArray[currentIndex];
        randomAnswerArray[currentIndex] = randomAnswerArray[randomIndex];
        randomAnswerArray[randomIndex] = temporaryValue;
    }
}

JavaScript JavaScript的

 var questionList = []; var randomAnswerArray = []; // this is the question object constructor function quizQuestion(question, correctAnswer, incorrectAnswer1, incorrectAnswer2, incorrectAnswer3) { this.question = question; this.correctAnswer = correctAnswer; this.incorrectAnswer1 = incorrectAnswer1; this.incorrectAnswer2 = incorrectAnswer2; this.incorrectAnswer3 = incorrectAnswer3; } // this constructs a question var hairColor = new quizQuestion("What color is my hair?", "black", "blue", "red", "purple"); // this adds the question to the questionList questionList.push(hairColor); document.getElementById('test2').innerHTML = questionList[0].correctAnswer; //TESTING object constructor works function generate() { // this part picks a random question var randomQuestion = questionList[Math.floor(Math.random() * questionList.length)]; // this part puts the question in the questionString document.getElementById("questionString").innerHTML = randomQuestion.question; // this part puts the answers in the array // THIS IS WHAT WE ARE TESTING VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV randomAnswerArray = []; //resets the array to empty randomAnswerArray.push(randomQuestion.correctAnswer); document.getElementById('test3').innerHTML=randomAnswerArray[0]; //TESTING doesn't work randomAnswerArray.push(randomQuestion.incorrectAnswer1); randomAnswerArray.push(randomQuestion.incorrectanswer2); randomAnswerArray.push(randomQuestion.incorrectanswer3); document.getElementById('test1').innerHTML = randomAnswerArray.valueOf(); //TESTING doesn't work // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // this part randomizes the array var currentIndex = randomAnswerArray.length; var temporaryValue; var randomIndex; while (0 !== currentIndex) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; temporaryValue = randomAnswerArray[currentIndex]; randomAnswerArray[currentIndex] = randomAnswerArray[randomIndex]; randomAnswerArray[randomIndex] = temporaryValue; } } 

Well, the function generate does not exist because JsFiddle adds the javascript code as function in the onLoad event. 嗯,函数生成不存在,因为JsFiddle在onLoad事件中将javascript代码添加为函数。 This make the generate function to be declared in a different private context. 这使得generate函数可以在其他私有上下文中声明。 I suggest to add the js code 我建议添加js代码

window.generate = generate;

as the last last of your code. 作为代码的最后一个。 This way you will set the generate function in the global namespace. 这样,您将在全局名称空间中设置generate函数。 and your onClick event will know call it properly. 并且您的onClick事件将正确调用它。 If you fix this, your script will run properly. 如果解决此问题,脚本将正常运行。

I also updated your fiddle for you: http://fiddle.jshell.net/GF9nj/9/ 我也为您更新了小提琴: http : //fiddle.jshell.net/GF9nj/9/

So here's the issue. 这就是问题所在。 It's a JSFiddle thing. 这是JSFiddle的事情。 Set the no wrap in body under Framework and Extensions option. 在Framework and Extensions选项下设置no wrap in body。 The way the javascript is being loaded, your function is not visible in the scope like you think it is. javascript的加载方式,您的函数在您所认为的范围内不可见。 I can explain further in an edit if you'd like 如果您愿意,我可以在编辑中进一步解释

JSFiddle wraps your JS in the document's onload event by default. 默认情况下,JSFiddle将JS包装在文档的onload事件中。 So your function was not defined in the root scope as you thought, but in the scope of the document.onload function, and you couldn't reach it from within the body, because that is outside of that function. 因此,您的功能不是在您想像的根范围内定义的,而是在document.onload函数的范围内定义的,您无法从主体内部访问它,因为那是在该函数之外。 I changed the JsFiddle attribute 'wrap in' to 'no wrap (body)' and it worked. 我将JsFiddle属性'wrap in'更改为'no wrap(body)'并且它起作用了。

You really shouldn't bind your functions with the onclick tag. 您确实不应该将函数与onclick标记绑定。 A better solution would be to remove the onclick from the button, and replace it with an id="button" 更好的解决方案是从按钮中删除onclick,然后将其替换为id="button"

Then at the bottom of your javascript, document.getElementById('start').onclick=generate 然后在您的JavaScript的底部, document.getElementById('start').onclick=generate

in your push statements you misspell these 在您的推送语句中,您拼错了这些

 incorrectAnswer2
 incorrectAnswer3

as

incorrectanswer2
incorrectanswer3

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

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