Chartjs-Loop through array of objects in Node

1👍

Map through your object season array and return the ppg property. By using map you can avoid using the extra pointAvg array.

const input = {
  _id: '5e94 ac2d81fa5428b0323fc1',
  name: 'Naz Mitrou-Long',
  image: 'https://alchetron.com/cdn/naz-long-b96e0ac4-b460-4dd7-ab76-de54cccbd62-resize-750.jpeg',
  position: 'Guard',
  description: 'Threesus of Nazareth',
  __v: 6,
  weight: 218,
  hometown: 'Mississauga, ON',
  country: 'Canada',
  height_feet: 6,
  height_inches: 4,
  season: [{
      year: '2012-2013',
      grade: 'Freshman',
      gp: 18,
      gs: 0,
      mpg: 6.9,
      fg: 0.348,
      tp: 0.278,
      ft: 1,
      rpg: 0.8,
      apg: 1,
      spg: 0.3,
      bpg: 0,
      ppg: 1.4
    },
    {
      year: '2013-2014',
      grade: 'Soph*m*re',
      gp: 36,
      gs: 7,
      mpg: 20.3,
      fg: 0.432,
      tp: 0.4,
      ft: 0.643,
      rpg: 1.6,
      apg: 1.1,
      spg: 0.2,
      bpg: 0.1,
      ppg: 7.1
    },
    {
      year: '2014-2015',
      grade: 'Junior',
      gp: 34,
      gs: 33,
      mpg: 27.5,
      fg: 0.449,
      tp: 0.391,
      ft: 0.755,
      rpg: 2.9,
      apg: 2,
      spg: 0.8,
      bpg: 0.1,
      ppg: 10.1
    },
    {
      year: '2015-2016',
      grade: 'R. Senior',
      gp: 8,
      gs: 8,
      mpg: 31.6,
      fg: 0.425,
      tp: 0.291,
      ft: 0.6,
      rpg: 2.9,
      apg: 1.9,
      spg: 0.6,
      bpg: 0.3,
      ppg: 12
    },
    {
      year: '2016-2017',
      grade: 'Senior',
      gp: 35,
      gs: 35,
      mpg: 33.3,
      fg: 0.473,
      tp: 0.384,
      ft: 0.795,
      rpg: 4.6,
      apg: 2.7,
      spg: 1.2,
      bpg: 0,
      ppg: 15.1
    }
  ]
}

const result = input.season.map(({
  ppg
}) => ppg);
console.log(result)

Leave a comment