Skip to content Skip to sidebar Skip to footer

Get Top Three Objects By Object Property In Array

I have an array of objects with object properties 'vote_average' and 'file_path', I'm trying to find three objects that have the highest 'vote_average' value, I still need the 'fil

Solution 1:

Sort the array in descending order by the "vote_average" property and then take a slice of the array.

const objList = [{
    vote_average: 1221,
    file_path: 'dsf'
  },
  {
    vote_average: 100,
    file_path: 'asdf'
  },
  {
    vote_average: 32,
    file_path: 'hgk'
  },
    {
    vote_average: 1,
    file_path: 'hgk'
  }
]

console.log(objList)

objList.sort(function(a, b) {
  return b.vote_average - a.vote_average
})

console.log(objList.slice(0,3))

Post a Comment for "Get Top Three Objects By Object Property In Array"