Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
How Do you Rank a Dictionary of Numbers From Local Storage in Javascript?
for(var i=0, len=localStorage.length; i<len; i++) {
        var key = localStorage.key(i);
        var value = JSON.parse(localStorage.getItem(key));
        console.log(key + " => " + value);
        let users = value.username;
        let scores = value.score;
        let names = value.name;

Image of the local storage:

enter image description here

The Code Above is how i access the local storage values e.g. value.score (Variable scores) will give the users score. on another webpage i have a leader board table and I want to rank the scores e.g. the user Meanace to be rank 1 and James to be rank 2 as he has a lower score. i Would also like to do that for all new users as well, and store the top 3 scores for other uses on the page thanks How would i Do that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Alright guys i managed to figure it out so what I did was store everything in a dictionary using key-value pairs then sort out the ranking by using a loop and pushing the value rank to each dictionary , then to call it I used a for loop to loop through the main array players

players=[];
for(var i=0, len=localStorage.length; i<len; i++) {
        var key = localStorage.key(i);
        var value = JSON.parse(localStorage.getItem(key));
        console.log(key + " => " + value);
        players.push({
          username: value.username,
          score: value.score,
           name: value.name
      });

      players.sort(function(a, b){
        return b.score - a.score;
    });
    
    var rank = 1;
    for (var i = 0; i < players.length; i++) {
      // increase rank only if current score less than previous
      if (i > 0 && players[i].score < players[i - 1].score) {
        rank++;
      }
        players[i].rank = rank;
    }
}
for(var i=0; i<players.length; i++) {
      let users = players[i].username;
      let scores = players[i].score;
      let names = players[i].name;
      let ranks = players[i].rank;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!