gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:38:04 25 4
gpt4 key购买 nike

这是一个 fiddle 。 http://fiddle.jshell.net/GF9nj/1/我试图弄清楚为什么当我将对象数据插入 randomAnswerArray 时,即使在测试

中它也不会显示。它大约位于 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。


这里是 fiddle 的完整代码:

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

最佳答案

嗯,函数generate不存在,因为JsFiddle在onLoad事件中添加了javascript代码作为函数。这使得生成函数在不同的私有(private)上下文中声明。我建议添加js代码

window.generate = generate;

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

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

关于javascript - 如何在 JavaScript 中将对象属性添加到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com