Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda