score:1

Accepted answer
  const rat = 900;
  const gamesList = [
    { gameId: "randomId1", creatorRating: 555 },
    { gameId: "randomId2", creatorRating: 666 },
    { gameId: "randomId3", creatorRating: 777 }
  ];
  const getElement = () => {
    let indexElement = 0;
    let differences = Math.abs(gamesList[0].creatorRating - rat);

    for (const el of gamesList) {
      if (el.creatorRating === rat) {
        indexElement = gamesList.indexOf(el);
        break;
      } else {
        const newDiff = Math.abs(el.creatorRating - rat);
        if (newDiff < differences) {
          differences = newDiff;
          indexElement = gamesList.indexOf(el);
        }
      }
    }
    return indexElement;
  };

Related Query

More Query from same tag