Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
get the first three items having the highest ratings from array

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],
    },
  ]);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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]
about 4 years ago · Juan Pablo Isaza Denunciar

0

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);

about 4 years ago · Juan Pablo Isaza Denunciar

0

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;
    })
));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda