首页 > 解决方案 > 如何从对象数组中检查对象中值的长度

问题描述

使用过滤器()

常量结果=运动员数据.filter(排序=>排序.收入.长度> 7);

标签: javascriptarraysobjectecmascript-6

解决方案


我刚刚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)让你可以看到数据。


推荐阅读