通过特定属性过滤和排列[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过特定属性过滤和排列[关闭]相关的知识,希望对你有一定的参考价值。

您好,我有以下对象的javascript数组:

  runners = [
      { id: 1, first_name: "Charmain", last_name: "Seiler", email: "cseiler0@wired.com", shirt_size: "2XL", company_name: "Divanoodle", donation: 75 },
      { id: 2, first_name: "Whitaker", last_name: "Ierland", email: "wierland1@angelfire.com", shirt_size: "2XL", company_name: "Wordtune", donation: 148 },
      { id: 3, first_name: "Julieta", last_name: "McCloid", email: "jmccloid2@yahoo.com", shirt_size: "S", company_name: "Riffpedia", donation: 171 },
      { id: 4, first_name: "Martynne", last_name: "Paye", email: "mpaye3@sciencedaily.com", shirt_size: "XL", company_name: "Wordware", donation: 288 },
      { id: 5, first_name: "Gussy", last_name: "Raraty", email: "graraty4@ucoz.ru", shirt_size: "L", company_name: "Oozz", donation: 291 },
  ]; 

我正在尝试解决以下挑战,我真的不知道我在做什么错。如果有人可以帮助,这是我设法写的,我知道这是不正确的,但我不知道为什么。

 /**
     * ### Challenge `getRunnersByTShirtSize`
     * 
     * @instructions
     * The event director needs a way to find the runners that need
     * a specific t-shirt size, so they can place the orders easily.
     * Implement this function using filter().
     * 
     * @param runners array of runners like the one inside the /data/runners.js file.
     * @param tShirtSize string (possible values are "S", "M", "L", "XL", "2XL", "3XL").
     * @returns an array containing only the runners that use the given `tShirtSize`.
     * The runners in the array appear in the same order they appear in the `runners` array.
    */
    function getRunnersByTShirtSize(runners, tShirtSize) {
      /* CODE HERE */
      const newSize =  runners.filter((size) => {
        return runners.size == tShirtSize;  
      });
      return newSize;
    }
答案

应该是runner.shirt_size而不是runners.size并且您应该引用在函数filter((runner)

中传递的项目
function getRunnersByTShirtSize(runners, tShirtSize) {
      /* CODE HERE */
      const newSize =  runners.filter((runner) => {
        return runner.shirt_size == tShirtSize;   //<===== not runners.size but instead shirt_size
      });
      return newSize;
    }

您还可以像这样摆脱多余的变量:

function getRunnersByTShirtSize(runners, tShirtSize) {
     return runners.filter(runners=> runners.shirt_size == tShirtSize);
 }
另一答案

filter方法接受一个回调,它是一个将数组元素作为第一个参数的函数。它使您可以遍历数组并过滤掉不需要的元素。这样做,回调函数必须返回一个布尔值:true将元素保留在返回的数组中,false将其保留。

这里您正在将数组每个元素的属性shirt_size与函数的第二个参数进行比较。因此,回调必须返回属性shirt_size等于tShirtSize

function getRunnersByTShirtSize(runners, tShirtSize) {
  return runners.filter((runner) => {
    return runner.shirt_size == tShirtSize;
  });
}

console.log(getRunnersByTShirtSize(runners, '2XL'))
<script>const runners=[{id:1,first_name:"Charmain",last_name:"Seiler",email:"cseiler0@wired.com",shirt_size:"2XL",company_name:"Divanoodle",donation:75},{id:2,first_name:"Whitaker",last_name:"Ierland",email:"wierland1@angelfire.com",shirt_size:"2XL",company_name:"Wordtune",donation:148},{id:3,first_name:"Julieta",last_name:"McCloid",email:"jmccloid2@yahoo.com",shirt_size:"S",company_name:"Riffpedia",donation:171},{id:4,first_name:"Martynne",last_name:"Paye",email:"mpaye3@sciencedaily.com",shirt_size:"XL",company_name:"Wordware",donation:288},{id:5,first_name:"Gussy",last_name:"Raraty",email:"graraty4@ucoz.ru",shirt_size:"L",company_name:"Oozz",donation:291}];</script>

以上是关于通过特定属性过滤和排列[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

基于内部片段的graphql过滤器(gatsbyJS)

解决方案电影标题中缺少代码的片段,完成挑战更多[关闭]

如何在片段中使用按钮[关闭]

在应用关闭时单击 Firebase 通知后打开特定的活动/片段

如何通过主题标签过滤和排列推文?

使用数据框的子集和 spark/scala 中的两个特定字段过滤数据框 [关闭]