How to create an object containing a range of values for a key

I’m trying to create an object that should hold a range of values for a key which I would use as a look-up table. For example, it needs to “catch” values in the ranges: 500-524, 600-650, etc..

e.g.:

const numbers = {
  500 to 524: "20",
  600 to 650: "25"
}

And I would like to access the value in the following way:

user.list.map(list => numbers[user.points]).

I know I can put all the values from the range as keys but that would be highly inefficient:

const numbers = {
  "500": "20",
  "501": "20",
  "502": "20",
  "503": "20",
  (...)
}

So, is it possible to include ranges somehow?

Answer

You could set it up like this:

numbers = [
  {"start": 500, "end": 524, "value": "20"},
  ...
  ...
]

function getFromNumbers(num) {
  for(let i=0; i<numbers.length; i++) {
    numVal = numbers[i];
    if(numVal.start <= num && numVal.end >= num)
      return numVal.value;
  }
}

console.log(user.list.map(list => getFromNumbers(user.points)))