Folks, I am extracting data from my collection into an array like so
obj = [{
"PlayerName": "Satyajit Sahay",
"Points": 83.83,
"Rank": 1
},
{
"PlayerName": "Chirag Galundia",
"Points": 75.69,
"Rank": 2
},
{
"PlayerName": "Kashyap Kapoor",
"Points": 70.91,
"Rank": 3
}]
Around 190 such records. My effort is to iterate the array in a for loop. Then identify if the points are the same. If same I am assigning the same rank or else I am assigning a rank +1. Here is the code written so far
var ranking = 1
for (i = 0; i < obj[0].length; i++) {
if (i > 0 && obj[0][i].Points < obj[0][i - 1].Points) {
ranking++
obj[0][i].Rank = ranking;
} else if (i > 0 && obj[0][i].Points === obj[0][i - 1].Points) {
obj[0][i].Rank = ranking;
ranking++
}
}
This works fine if 2 consecutive players have the same points. The minute the third player has the same rank it behaves weirdly. Can someone help?