Home > database >  Retrieve object value considering a key value range
Retrieve object value considering a key value range

Time:07-25

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

CodePudding user response:

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)
  );
});

CodePudding user response:

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);

CodePudding user response:

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.

  • Related