如何从对象的错误检查对象中值的长度

时间:2019-01-02 01:40:31

标签: javascript arrays object ecmascript-6

使用filter()

  • 使用运动数据数组和.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结果=运动者数据过滤器(已排序=> sorted.income.length> 7);

1 个答案:

答案 0 :(得分: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),以便您可以查看数据。