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

187
Visualizações
How to compare and get object value in javascript object

I have this JS object ( data will be diffrent , it's just for example )

{10: 0, 
    30: 2.5, 
    365: 15, 
    730: 25, 
    1095: 35}

Key is Days and value is percentage. Now if input is between 0 to 10 days then percentage will be 0, if between 11 to 30 days then percentage will be 2.5 if between 31 to 365 days then percentage will be 15 if 1100 days then 35

here is my code

days - > input 
obj_data - object 
    jQuery.each(obj_data, function(key, val) {
        if( days <= key ){
            console.log(val);
            return false;
        }
    });
    return true

here how to get value if days are greater than to 1095

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

By adding a value that is higher than anything else for what's possible, you have a value you can check for. If that value is returned, get the "largest"/"last" value from the provided values.

In the example below I use concat to add the largest safe integer to the array, so the compare function keeps running.

If that value is returned, we know we didn't have a match, so we get the last value from the provided object.

let data = {10: 0, 
    30: 2.5, 
    365: 15, 
    730: 25, 
    1095: 35}
let days = [1,11,31,366,731,1096]
function find(day, obj) 
{
  let keys = Object.keys(obj);
  result = keys.concat(Number.MAX_SAFE_INTEGER).filter(key => {
     return day <= key;
  }).shift();
  
  if(result === Number.MAX_SAFE_INTEGER) {
     result = keys.pop();
  }
  return obj[result];
}
days.forEach(day => console.log(day, " gets value:",find(day, data)))

about 4 years ago · Santiago Gelvez 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