猿问

按参数过滤数组

我有一个数组:


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。想法?


守候你守候我
浏览 92回答 3
3回答

当年话下

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}],&nbsp; &nbsp; filterQuestionsByType = (submissions, essayquestions) =>&nbsp;&nbsp; &nbsp; &nbsp; submissions.filter(({isEssayQuestion}) => isEssayQuestion == essayquestions)&nbsp; &nbsp; &nbsp;&nbsp;console.log(filterQuestionsByType(src, true))

人到中年有点甜

那么就使用.filter这个案例的方法const submissions = [&nbsp; {&nbsp; &nbsp; question: 'blah blah blah ?',&nbsp; &nbsp; response: 'blah blah blah',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: false&nbsp; },&nbsp; {&nbsp; &nbsp; question: 'blah blah blah blah',&nbsp; &nbsp; response: 'blah blah blah',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: false&nbsp; },&nbsp; {&nbsp; &nbsp; question: 'blah blah blah ?',&nbsp; &nbsp; response: 'blah blah blah blah',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: true&nbsp; },&nbsp; {&nbsp; &nbsp; question: 'True or False: I am new to JS.',&nbsp; &nbsp; response: 'True',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: false&nbsp; }];function essays(essay, question) {&nbsp; &nbsp;return essay.filter(({isEssayQuestion}) => isEssayQuestion === question)}console.log(essays(submissions, false));console.log("------------------------------------");console.log(essays(submissions, true));

慕妹3146593

这是另一种方法。您可以简单地将源数组映射到新对象属性 (sortedValues) 并将结果写入其中。此方法只运行一次,而不是其他示例,您需要运行两次 - 真和假。const submissions = [&nbsp; {&nbsp; &nbsp; question: 'blah blah blah ?',&nbsp; &nbsp; response: 'blah blah blah',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: false&nbsp; },&nbsp; {&nbsp; &nbsp; question: 'blah blah blah blah',&nbsp; &nbsp; response: 'blah blah blah',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: false&nbsp; },&nbsp; {&nbsp; &nbsp; question: 'blah blah blah ?',&nbsp; &nbsp; response: 'blah blah blah blah',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: true&nbsp; },&nbsp; {&nbsp; &nbsp; question: 'True or False: I am new to JS.',&nbsp; &nbsp; response: 'True',&nbsp; &nbsp; isCorrect: true,&nbsp; &nbsp; isEssayQuestion: false&nbsp; }];const sortedValues = {&nbsp; trueValues: [],&nbsp; falseValues: []}submissions.map(item => {&nbsp; sortedValues[`${item.isEssayQuestion}Values`] = [...sortedValues[`${item.isEssayQuestion}Values`], item]})console.log(JSON.stringify(sortedValues, 2,2))
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答