简体   繁体   English

如何定位数组 object 中的 boolean 值。 JavaScript

[英]How to target boolean value in array object. JavaScript

 let myQuestions = [ { question: "What's 2+2?", answers: [ { text: "4", correct: true }, { text: "2", correct: false }, { text: "10", correct: false }, { text: "1", correct: false }, ], }, { question: "What's 10+10?", answers: [ { text: "20", correct: true }, { text: "2", correct: false }, { text: "18", correct: false }, { text: "0", correct: false }, ], }, ]; function showQuestion(questionAndAnswers) { const shuffledAnswers = _.shuffle(questionAndAnswers.answers); questionTag.innerText = questionAndAnswers.question; answerTag[0].innerText = shuffledAnswers[0].text; answerTag[1].innerText = shuffledAnswers[1].text; answerTag[2].innerText = shuffledAnswers[2].text; answerTag[3].innerText = shuffledAnswers[3].text; } document.querySelectorAll(".answer").forEach((correctAnswer) => { correctAnswer.addEventListener("click", (event) => { if (myQuestions.answers == true) { console.log("s"); } }); })

Basically, I have my questions in an array with the answers in an object too, the question get put on the screen with the answers shuffled.基本上,我的问题在一个数组中,答案也在 object 中,问题被放在屏幕上,答案被打乱了。 What I need help with is the last block of code below, how can I check if the value clicked is true(correct answer).我需要帮助的是下面的最后一段代码,如何检查单击的值是否为真(正确答案)。

 <h3 id="question"></h3> <div class="answers"> <button id="answer1" class="answer"></button> <button id="answer2" class="answer"></button> <button id="answer3" class="answer"></button> <button id="answer4" class="answer"></button> </div>

Add an attribute to the answer tags indicating whether it's the correct answer.向答案标签添加一个属性,指示它是否是正确答案。 Then you can check that in the event listener.然后你可以在事件监听器中检查。

 let myQuestions = [ { question: "What's 2+2?", answers: [ { text: "4", correct: true }, { text: "2", correct: false }, { text: "10", correct: false }, { text: "1", correct: false }, ], }, { question: "What's 10+10?", answers: [ { text: "20", correct: true }, { text: "2", correct: false }, { text: "18", correct: false }, { text: "0", correct: false }, ], }, ]; function showQuestion(questionAndAnswers) { const shuffledAnswers = _.shuffle(questionAndAnswers.answers); questionTag.innerText = questionAndAnswers.question; questionAndAnswers.answers.forEach(({text, correct}, i) => { answerTag[i].innerText = text; answerTag[i].dataset.correct = correct; }; } document.querySelectorAll(".answer").forEach((answer) => { answer.addEventListener("click", (event) => { if (event.target.dataset.correct == 'true') { console.log("s"); } }); })

暂无
暂无

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

相关问题 如何根据数组对象javascript中的条件检查返回布尔值 - how to return boolean value based on condition check in array object javascript Javascript 和 window.document object。 如何更改文档中的值 - Javascript and the window.document object. How to change value in document JavaScript 测试:将 Boolean 值更改为 object 数组 - JavaScript testing: change Boolean value into an array of object jQuery的返回值是Array,但构造函数是Object。 怎么样? - jQuery's return value is Array, but constructor is Object. how? 我如何循环遍历数组中每个 object 的属性,每个 object 的属性都不同。 这是 Javascript - How do i loop through the properties of each object in the array, the properties are different in each object. This is Javascript 如何将数组值作为对象键传递,并将该键与对象中的某些值相关联。 然后将其传递给列表项 - how to pass an array value as an object key, and associate that key with some values in the object. and then pass it into a list item 如何使用 javascript 或 lodash 返回指示 object 数组元素为空的 boolean 值 - How to return a boolean value indicating an element of an array of object is empty using javascript or lodash 交换一个Objects值,以及另一个对象的值。 * *的Javascript - Swapping an Objects value, with a value from another object. *Javascript* JavaScript - 改进 object 的动态数组。 保持重复 - JavaScript - Improvement of the dynamic array of object. Maintenance of repetitions JavaScript Object。 更改数组中对象的键名 - JavaScript Object. Change key name of objects in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM