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

时间:2014-07-25 15:05:59

标签: javascript arrays html5 object

这是一个小提琴。 http://fiddle.jshell.net/GF9nj/1/ 我试图找出的是为什么当我将我的对象数据推送到randomAnswerArray中时,即使在测试< p>中它也不会出现。它大约是JavaScript部分的一半。

以下是相关代码:

// 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();

valueOf()调用只显示来自randomAnswerArray的0和1项,但是我推动了2& 3以及。


以下是小提琴的完整代码:

HTML

<p id='questionString'></p>
<p id='test1'></p>
<p id='test2'></p>
<p id='test3'></p>
<button onclick='generate()'>Click me to start!</button>

的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;
    }
}

3 个答案:

答案 0 :(得分:2)

好吧,函数generate不存在,因为JsFiddle在onLoad事件中将javascript代码添加为函数。这使得generate函数在不同的私有上下文中声明。 我建议添加js代码

window.generate = generate;

作为最后一个代码。这样,您将在全局命名空间中设置generate函数。并且您的onClick事件将知道正确调用它。 如果您解决了这个问题,您的脚本将正常运行。

我还为您更新了小提琴:http://fiddle.jshell.net/GF9nj/9/

答案 1 :(得分:1)

所以这就是问题所在。它是一个JSFiddle的东西。在Framework and Extensions选项下设置body中的no wrap。加载javascript的方式,您的功能在范围内不可见,就像您认为的那样。如果你想要

,我可以在编辑中进一步解释

默认情况下,JSFiddle将JS包装在文档的onload事件中。所以你的函数没有像你想象的那样在根作用域中定义,但是在document.onload函数的范围内,你无法从正文中找到它,因为那是在函数之外。我更改了JsFiddle属性&#39;换入&#39;没有包裹(身体)&#39;它起作用了。

你真的不应该用onclick标签绑定你的函数。更好的解决方案是从按钮中删除onclick,并将其替换为id="button"

然后在您的javascript底部document.getElementById('start').onclick=generate

答案 2 :(得分:1)

在你的推送声明中拼错了这些

 incorrectAnswer2
 incorrectAnswer3

as

incorrectanswer2
incorrectanswer3