【问题标题】:How to check the length of value in object from arrray of object如何从对象数组中检查对象中值的长度
【发布时间】:2019-05-28 17:54:56
【问题描述】:

使用过滤器()

  • 使用运动员数据数组和.filter():

  • 仅返回运动员收入超过 7 个字符的运动员对象

  • 将返回的数据存储在一个新的results变量中 *
  • 注意:
  • 不要删除运动员数据变量

  • 不要更改任何运动员数据内容

    const athleteData = [
     { athlete: 'Lionel Messi', team: 'Barcelona', income: 40000000 },
    
    
     { athlete: 'Cristiano Ronaldo', team: 'Juventus', income: 30000000 },
    
    
     { athlete: 'Neymar', team: 'Paris Saint-Germain', income: 36800000 },
    
    
     { athlete: 'Eden Hazard', team: 'Chelsea', income: 10400000 },
    
    
     { athlete: 'Mohamed Salah', team: 'Liverpool', income: 4680000 },
    
    
     { athlete: 'Kylian Mbappé', team: 'Paris Saint-Germain: An American Musical', income: 17500000 },
    
     { athlete: 'Luka Modrić', team: 'Real Madrid', income: 9360000 },
    
     { athlete: 'Harry Kane', team: 'Tottenham Hotspurs', income: 17600000 },
    
     { athlete: 'Kevin De Bruyne', team: 'Manchester City', income: 5980000 },
    
     { athlete: 'Paul Pogba', team: 'Manchester United', income: 15080000 }
    
    ];
    
    const results = 'Replace this message with your code!';
    
    console.log(results);
    

    ** 我想做的方式是**:

const results = playerData.filter(sorted => sorted.income.length > 7);

【问题讨论】:

  • 发布的问题似乎根本没有包含any attempt 来解决问题。 StackOverflow 期待您 try to solve your own problem first,因为您的尝试有助于我们更好地了解您想要什么。请编辑问题以显示您尝试过的内容,以说明您遇到minimal reproducible example 的特定障碍。欲了解更多信息,请参阅How to Ask 并拨打tour
  • 它看起来像一个作业,所以这可能是你得到的,但是基于数字字符数的过滤很奇怪。例如1000.00 有 7 个字符,但这可能不是您想要的。

标签: javascript arrays object ecmascript-6


【解决方案1】:

我刚刚按照你的规则做了一个filter声明:

const athleteData = [{
    athlete: 'Lionel Messi',
    team: 'Barcelona',
    income: 40000000
  },

  {
    athlete: 'Cristiano Ronaldo',
    team: 'Juventus',
    income: 30000000
  },

  {
    athlete: 'Neymar',
    team: 'Paris Saint-Germain',
    income: 36800000
  },

  {
    athlete: 'Eden Hazard',
    team: 'Chelsea',
    income: 10400000
  },

  {
    athlete: 'Mohamed Salah',
    team: 'Liverpool',
    income: 4680000
  },

  {
    athlete: 'Kylian Mbappé',
    team: 'Paris Saint-Germain: An American Musical',
    income: 17500000
  },

  {
    athlete: 'Luka Modrić',
    team: 'Real Madrid',
    income: 9360000
  },

  {
    athlete: 'Harry Kane',
    team: 'Tottenham Hotspurs',
    income: 17600000
  },

  {
    athlete: 'Kevin De Bruyne',
    team: 'Manchester City',
    income: 5980000
  },

  {
    athlete: 'Paul Pogba',
    team: 'Manchester United',
    income: 15080000
  }

];

const results = athleteData.filter(({ income }) => income.toString().length > 7);

console.log(results);

这样过滤athleteData

首先,我们解构 ({ income }) 以获取每个对象的 income 属性。

接下来,我们将其转换为字符串 (.toString()),以便我们检查每个对象的 length

然后我们只需 console.log(results) 以便您查看数据。

【讨论】:

  • 感谢杰克我正在尝试做的事情是,const results = playerData.filter(sorted => sorted.income.length > 7);请指出错误及其替代方法。
  • 您正在尝试检测数字的长度是否超过 7 - 数字没有长度,这就是我首先转换为字符串的原因。
  • 你不能这样做<Number>.length,因为长度不是原型上存在的属性,所以使用.toString()是这样做的合适方法
  • @UbaidUllah 你可能不得不做parseInt(income).toString().length 以防万一任何收入有小数点。
猜你喜欢
  • 2019-05-19
  • 2016-09-29
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多