Home > database >  get the first three items having the highest ratings from array
get the first three items having the highest ratings from array

Time:07-03

hello i have a question. i need to get the 3 object that has the highest rating (for now i have 3 by it dosent matter because i need to add more) how can i do it? i am trying to use sort and spread but because that is array of object and another array (rating) i get confused thanks

const [movies, setMovies] = useState([
    {
      movieName: "godfather",
      info: "the rise of  corelone family  all mater is respect,money,woman but the youngest son michal has a diffrent plans...",
      picture: "pictures/godfather.jpg",
      rating: [10, 5, 1],
    },
    {
      movieName: "scarface",
      info: "the rise of  tony montata the badass man of miami want to control miami and be the number 1 of the cocaine provider ...",
      picture: "pictures/scarface.jpg",
      rating: [10, 9, 2],
    },
    {
      movieName: "the-dark-knight",
      info: "you all knew that he will come the batman! come to fight with his popular rival the joker...",
      picture: "pictures/the-dark-knight-poster.jpg",
      rating: [10, 10, 9],
    },
  ]);

CodePudding user response:

Like this

const rates = movies
  .reduce((acc, curr) => {
    const sumRate = curr.rating.reduce((acc2, curr2) => acc2   curr2, 0)
    const rate = sumRate / curr.rating.length
    acc.push({ ...curr, rate })
    return acc
  }, [])
  .sort((a, b) => a.rate - b.rate)

const highestRate = rates[rates.length - 1]

CodePudding user response:

This is the most efficient way to get only the first three highest-rated items with O(n) time (without sorting the whole array). The idea here is you don't want to sort the whole array. Only update pointers to the three largest items.

When you find a higher value for the first item then just give the old first item value to the second item, and the third item the second item (because the second item is getting updated with a larger value and the third item can take its old value).

const movies = [
  {
    movieName: "the-dark-knight",
    info: "you all knew that he will come the batman! come to fight with his popular rival the joker...",
    picture: "pictures/the-dark-knight-poster.jpg",
    rating: [10, 10, 9],
  },
  {
    movieName: "godfather",
    info: "the rise of  corelone family  all mater is respect,money,woman but the youngest son michal has a diffrent plans...",
    picture: "pictures/godfather.jpg",
    rating: [10, 5, 1],
  },
  {
    movieName: "scarface",
    info: "the rise of  tony montata the badass man of miami want to control miami and be the number 1 of the cocaine provider ...",
    picture: "pictures/scarface.jpg",
    rating: [10, 9, 2],
  },
];

let first = { rating: [Number.NEGATIVE_INFINITY] };
let second = { rating: [Number.NEGATIVE_INFINITY] };
let third = { rating: [Number.NEGATIVE_INFINITY] };

const getTotalRating = (rating) => {
  return rating.reduce((prev, curr) => prev   curr, 0);
};

movies.forEach((currItem) => {
  const currItemTotalRating = getTotalRating(currItem.rating);
  const firstTotalRating = getTotalRating(first.rating);
  const secondTotalRating = getTotalRating(second.rating);
  const thirdTotalRating = getTotalRating(third.rating);

  if (currItemTotalRating > firstTotalRating) {
    third = second;
    second = first;
    first = currItem;
  } else if (currItemTotalRating > secondTotalRating) {
    third = second;
    second = currItem;
  } else if (currItemTotalRating > thirdTotalRating) {
    third = currItem;
  }
});

console.log(first);
console.log(second);
console.log(third);

CodePudding user response:

This is how you can sort by average and total rating using the sort method (docs).

Sort by average rating

const getAverage = (array) => {
    const total = array.reduce((a, b) => a   b);
    return total / array.length;
};

setMovies(prev => (
    prev.sort((a, b) => {
        const aRating = a.rating;
        const bRating = b.rating;
        if (getAverage(aRating) > getAverage(bRating)) return -1;
        if (getAverage(aRating) < getAverage(bRating)) return 1;
        return 0;
    })
));

Sort by total rating

const getTotal = (array) => {
    return array.reduce((a, b) => a   b);
};

setMovies(prev => (
    prev.sort((a, b) => {
        const aRating = a.rating;
        const bRating = b.rating;
        if (getTotal(aRating) > getTotal(bRating)) return -1;
        if (getTotal(aRating) < getTotal(bRating)) return 1;
        return 0;
    })
));
  • Related