我怎樣才能在這個物件中鎖定 "真 "的值呢?
span class="hljs-keyword">const questions = [
{
questionText:
'this is question 1',
answerOptions: [
{ answerText: 'answer1', isCorrect: false },
{ answerText: '答案2', isCorrect: false },
{ answerText: '答案3', isCorrect: true }。
{ answerText: 'answer4', isCorrect: false },
],
}
]
uj5u.com熱心網友回復:
。const questions = [
{
questionText:
'this is question 1',
answerOptions: [
{ answerText: 'answer1', isCorrect: false },
{ answerText: '答案2', isCorrect: false },
{ answerText: '答案3', isCorrect: true }。
{ answerText: 'answer4', isCorrect: false },
],
}
]
let allCorrects = [] 。
//你可以使用這個代碼塊。
allCorrects = questions.map(x => x.answerOptions. filter(y => y.isCorrect))。)
console.log(allCorrects)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以直接使用js中的filter方法,它回傳一個陣列的truthy值。
。 const questions = [
{
questionText:
'this is question 1',
answerOptions: [
{ answerText: 'answer1', isCorrect: false },
{ answerText: '答案2', isCorrect: false },
{ answerText: '答案3', isCorrect: true }。
{ answerText: 'answer4', isCorrect: false },
],
}
]
const result = questions[0].answerOptions。 filter(span class="hljs-params">answer => answer.isCorrect )
console.log(result)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329124.html
標籤:
