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

154
Visualizações
Using for in to iterate through objects values and return the highest value - Can I use it in the same way I would a for Loop

I am trying to return the mode of a passed array. I have read a lot about for in loops but due to es6 syntax used in these examples, I am struggling to understand how I can use this loop. My question: I want to return the key that has the highest value of all the object pairs.

I.e with the following object:

{ '4': 1, '6': 1, '7': 1, '8': 1, '10': 2, '12': 3 }

I want to return the key that contains the highest value. In this case it would be 12 since 3 is the highest value out of the pairs.

Is it possible to use a for in loop in a similar way to how I would iterate through an array to find the highest value. (I understand that non-array objects are not indexed in the same way arrays are, but I need to compare one key:value pair to the next in the loop and not sure how to code that). I.e:


let highest = 0;
      for(const x in count){
        if(count[x+1] > count[x]){
         highest = count[x][i+1];
       }

When I pass the code below it returns 0 so I assume the For In loop isn't working as expected.

Full Code for reference:


function highestRank(arr){
  
  const count = {};
  
  for(let i=0; i<arr.length; i++){
    if(count.hasOwnProperty(arr[i]) === false){
        count[arr[i]]= 1;
      }else{
        count[arr[i]] += 1;
      }
  }
 //code below is the issue
  let highest = 0;
  for(const x in count){
    if(count[x+1] > count[x]){
     highest = count[x][i+1];
   }
  } 
  
  return highest;
}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use Object.entries combined with reduce and in cumulator keep object with maxKey and maxValue;

const data = { '4': 1, '6': 1, '7': 1, '8': 1, '10': 2, '12': 3 };
const max = Object.entries(data).reduce(({maxKey, maxValue = -Infinity}, [key, value]) => {
  if(value > maxValue){
    maxValue = value;
    maxKey = key;
  }
  return {maxKey, maxValue}
}, {});

console.log(max);
Edit: (short version)

const data = { '4': 1, '6': 1, '7': 1, '8': 1, '10': 2, '12': 3 };
const max = Object.entries(data).reduce(([maxKey, maxValue], [key, value]) =>
  value > maxValue ? [key, value] : [maxKey, maxValue]
);

const maxShort = Object.entries(data).reduce((a, c) => c[1] > a[1]? c : a);


console.log(maxShort);

about 4 years ago · Juan Pablo Isaza Relatório

0

Traditional loop version, use variables greatest and its respective key, loop the object properties

const count = { '4': 1, '6': 1, '7': 1, '8': 1, '10': 2, '12': 3 };

let greatest = -Infinity;
let key;
for (let x in count) {
  if (count[x] > greatest) {
    key = x;
    greatest = count[key];
  }
}

console.log(key, greatest);

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