Как проверить длину значения в объекте из массива объекта - PullRequest
0 голосов
/ 02 января 2019

Использование фильтра ()

  • Использование массива athleteData и .filter ():

  • возвращать только объекты спортсмена, доход которых спортсменов равендлина более 7 символов

  • сохранить возвращенные данные в новой переменной results *
  • Примечание:
  • не удалять переменную athleteData

  • не изменяйте никакие данные о athleteData

    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 = athleteData.filter (sorted => sorted.income.length> 7);

1 Ответ

0 голосов
/ 02 января 2019

Я только что сделал заявление 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), чтобы вы моглисм. данные.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...