Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda