为了账号安全,请及时绑定邮箱和手机立即绑定

按参数过滤数组

按参数过滤数组

守候你守候我 2022-12-22 15:01:52
我有一个数组: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。想法?
查看完整描述

3 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

Array.prototype.filter()完全符合目的:



const src = [{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}],


    filterQuestionsByType = (submissions, essayquestions) => 

      submissions.filter(({isEssayQuestion}) => isEssayQuestion == essayquestions)

      

console.log(filterQuestionsByType(src, true))


查看完整回答
反对 回复 2022-12-22
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

那么就使用.filter这个案例的方法


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 essays(essay, question) {

   return essay.filter(({isEssayQuestion}) => isEssayQuestion === question)

}


console.log(essays(submissions, false));

console.log("------------------------------------");

console.log(essays(submissions, true));


查看完整回答
反对 回复 2022-12-22
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

这是另一种方法。您可以简单地将源数组映射到新对象属性 (sortedValues) 并将结果写入其中。此方法只运行一次,而不是其他示例,您需要运行两次 - 真和假。


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

  }

];


const sortedValues = {

  trueValues: [],

  falseValues: []

}



submissions.map(item => {

  sortedValues[`${item.isEssayQuestion}Values`] = [...sortedValues[`${item.isEssayQuestion}Values`], item]

})


console.log(JSON.stringify(sortedValues, 2,2))


查看完整回答
反对 回复 2022-12-22
  • 3 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信