💾arr.rank()

JSobjectsArraymethods ⟩ rank()

💾 replit: arr.rank()

// 🔸 arr.rank()
Array.prototype.rank = function(compare, consecutiveRanks=true, rankPropertyName='rank'){

  const sorted = this
      .slice()            // copy the array
      .sort(compare);     // sort array of objects (in place)
    
  let current;            // object keeping the current rank

  // 🧨 Note:
  // - indices (keys) of array.entries() are "integer-based"
  // - indices (keys) of Object.entries(array) are "string-based"❗❗❗
  for(const [i, obj] of sorted.entries()){  

    // if not the same, generate new rank
    if (!current || compare(current, obj) !== 0) {
      obj[rankPropertyName] = (
          !consecutiveRanks ? i + 1 :        // ranks may have "gaps"
          !current          ?     1 :        // first rank
          current[rankPropertyName] + 1      // consecutive ranks
      );
      current = obj;
    }
        
    // if the same, use the current rank.
    else {
      obj[rankPropertyName] = current[rankPropertyName];
    }

  }

  return sorted;
};

💈範例:

const players = [
    { nickname: "Bob"  , score: 100 },
    { nickname: "Amy"  , score: 200 },
    { nickname: "Grant", score: 300 },
    { nickname: "Steve", score: 200 },
    { nickname: "Joe"  , score: 500 },
];

console.log(players.rank((a,b) => b.score - a.score));
// [
//   { nickname: 'Joe'  , score: 500, rank: 1 },
//   { nickname: 'Grant', score: 300, rank: 2 },
//   { nickname: 'Amy'  , score: 200, rank: 3 },
//   { nickname: 'Steve', score: 200, rank: 3 },
//   { nickname: 'Bob'  , score: 100, rank: 4 }    // ⭐️ consecutive rank
// ]

console.log(players.rank((a,b) => b.score - a.score, false));
// [
//   { nickname: 'Joe'  , score: 500, rank: 1 },
//   { nickname: 'Grant', score: 300, rank: 2 },
//   { nickname: 'Amy'  , score: 200, rank: 3 },
//   { nickname: 'Steve', score: 200, rank: 3 },
//   { nickname: 'Bob'  , score: 100, rank: 5 }    // ⭐️ rank 4 is skipped.
// ]

console.log(players);
// [
//   { nickname: 'Bob'  , score: 100, rank: 5 },    // in original order
//   { nickname: 'Amy'  , score: 200, rank: 3 },
//   { nickname: 'Grant', score: 300, rank: 2 },
//   { nickname: 'Steve', score: 200, rank: 3 },
//   { nickname: 'Joe'  , score: 500, rank: 1 }
// ]

Last updated