我有一个数组:
const submissions = [
{
question: 'blah blah blah ?',
response: 'blah blah blah',
isCorrect: true,
isEssayQuestion: false
},
{
question: 'blah blah blah blah',
response: 'blah blah blah',
isCorrect: true,
isEssayQuestion: false
},
{
question: 'blah blah blah ?',
response: 'blah blah blah blah',
isCorrect: true,
isEssayQuestion: true
},
{
question: 'True or False: I am new to JS.',
response: 'True',
isCorrect: true,
isEssayQuestion: false
}
];
该函数接受提交数组和一个布尔参数。
function filterQuestionsByType(submissions, essayquestion) {
let trueEssay = [];
let falseEssay = [];
for (let i = 0; i < submissions.length; i++) {
if ( essayquestion === true) {
if ( submissions[i].isEssayQuestion === true) {
trueEssay.push(submissions[i]);
} else {
falseEssay.push(submissions[i]);
}
}
}
if (essayquestion === true) {
return trueEssay;
} else {
return falseEssay;
}
}
如果为真,我需要返回一个包含所有 isEssayQuestion 为真的条目的数组。如果为假,则相同。它适用于 true 但不是 false。想法?
当年话下
人到中年有点甜
慕妹3146593
相关分类