-1

Working on a NodeJS web app and am trying to get some information from my backend dataset. Here's what the pertinent part of the Mongoose model looks like:

gameLog: [
  {
     date: String,
     season: String,
     location: String,
     opp: String,
     gameType: String,
     result: String,
     start: Number,
     min: Number,
     fgm: Number,
     fga: Number,
     fgp: Number,
     twoPM: Number,
     twoPA: Number,
     twoP: Number,
     threePM: Number,
     threePA: Number,
     threeP: Number,
     ftm: Number,
     fta: Number,
     ftp: Number,
     orb: Number,
     drb: Number,
     ast: Number,
     stl: Number,
     blk: Number,
     to: Number,
     pf: Number,
     pts: Number
  }

]

It's for the storage of box scores. How can I search through each object's season field and return an array containing the unique values from that field? When I use a map the searches the data of the requested player on the route, it returns:

let seasons = foundPlayer.gameLog.map(({
    season}) => season);

// Output

[
  '2019', '2019', '2019', '2019', '2019',
  '2019', '2019', '2019', '2019', '2019',
  '2019', '2019', '2019', '2019', '2019',
  '2019', '2019', '2019', '2019', '2019',
  '2019', '2019', '2019', '2019', '2019',
  '2019', '2020', '2020', '2020', '2020',
  '2020', '2020', '2020', '2020', '2020',
  '2020', '2020', '2020', '2020', '2020',
  '2020', '2020', '2020', '2020', '2020',
  '2020', '2020', '2020', '2020', '2020',
  '2020', '2020', '2020', '2020', '2020',
  '2020', '2020', '2020'
]

So rather than each instance of the season in the array, I want the unique values. What am I missing here? Thanks in advance for the help!

Franchise
  • 1,081
  • 3
  • 15
  • 30

1 Answers1

1

Here's one way to do it:

let seasons = [...(new Set(foundPlayer.gameLog.map(({
    season}) => season)))];

We're using the native Set object, which always guarantees unique values. Another approach is to use an external library like lodash.

Karamell
  • 1,023
  • 9
  • 23