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

165
Vistas
Retrieve object value considering a key value range

Is it possible to retrieve an object value base on a key range condition?

For instance my Obj is

const ind_ind = {
            '25000':  200,
            '100000': 375,
            '200000': 750,
            '400000': 900
        }

The idea is to get the corresponding values considering those ranges

0 up to 25000;
25.001 up to 100.000; 
100.001 up to 200.000; 
above 200.001 

So the expected output for these e.g. entries are

input is 5.000  outputs 200
input is 24.999 outputs 200
input is 25.000 outputs 200 
input is 25.001 outputs 375
input is 99.999 outputs 375
input is 100.000 outputs 375
input is 100.001 outputs 750
input is 199.999 outputs 750
input is 200.000 outputs 750
input is 200.001 outputs 900
input is 399.999 outputs 900
input is 400.000 outputs 900
input is 400.001 or above outputs 900

I hope to have clarify it enough to get my answer.

Appreciate any help

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use Array.prototype.find() with the condition you want for the object keys.

Try like below:

const ind_ind = {
  25000: 200,
  100000: 375,
  200000: 750,
  400000: 900,
};

const findValueForKeyLimit = (keyLimit) => {
  const keys = Object.keys(ind_ind).map((value) => parseInt(value));

  // Edge case when key limit is less than the first key
  if (keyLimit <= keys[0]) {
    return ind_ind[keys[0]];
  }

  const key = keys.find(
    (key, keyIndex) =>
      key <= keyLimit &&
      (keyIndex === keys.length - 1 || keyLimit < keys[keyIndex + 1])
  );

  if (key) {
    return ind_ind[key];
  }
  return null;
};

[24000, 25100, 101000, 200000, 403000].forEach((testValue) => {
  console.log(
    "input: ",
    testValue,
    "=> output: ",
    findValueForKeyLimit(testValue)
  );
});

about 4 years ago · Santiago Trujillo Denunciar

0

const ind_ind = {
  25000: 200,
  100000: 375,
  200000: 750,
  400000: 900,
};

const customFind = (val, obj) =>
  Object.entries(obj).find(([k]) => val <= Number(k))?.[1] ??
  Object.values(obj).slice(-1)[0];

console.log(customFind(5000, ind_ind), "expected:", 200);
console.log(customFind(24999, ind_ind), "expected:", 200);
console.log(customFind(25000, ind_ind), "expected:", 200);
console.log(customFind(25001, ind_ind), "expected:", 375);
console.log(customFind(99999, ind_ind), "expected:", 375);
console.log(customFind(100000, ind_ind), "expected:", 375);
console.log(customFind(100001, ind_ind), "expected:", 750);
console.log(customFind(199999, ind_ind), "expected:", 750);
console.log(customFind(200000, ind_ind), "expected:", 750);
console.log(customFind(200001, ind_ind), "expected:", 900);
console.log(customFind(399999, ind_ind), "expected:", 900);
console.log(customFind(400000, ind_ind), "expected:", 900);
console.log(customFind(400001, ind_ind), "expected:", 900);

about 4 years ago · Santiago Trujillo Denunciar

0

Is it possible to retrieve an object value base on a key range condition?

To search ranges, either use a linear_search for a smallish number of keys or use bisection for a larger number of keys.

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