Ecmascript 6 多个范围-我的值在哪个范围内?

Ecmascript 6 多个范围-我的值在哪个范围内?,ecmascript-6,switch-statement,range,Ecmascript 6,Switch Statement,Range,有没有聪明的方法来检查我的号码在哪个范围内?我正在使用React with ES6。我有一个不同范围的列表,每个范围定义了它可以拥有的值。例如: if my number is above 0 but less than 0.5, the color should be red if my number is above 0.5 and less than 1, the color should be blue if my number is above 1 and less than 5, t

有没有聪明的方法来检查我的号码在哪个范围内?我正在使用React with ES6。我有一个不同范围的列表,每个范围定义了它可以拥有的值。例如:

if my number is above 0 but less than 0.5, the color should be red
if my number is above 0.5 and less than 1, the color should be blue
if my number is above 1 and less than 5, the color should be green
if my number is above 5 and less than 10, the color should be purple
范围可以变化,因此没有静态范围列表。我想象的是一个开关状态,它可以接受一个范围列表,其中包含一个对应的值或函数,当情况为真时应该运行该值或函数。我不想要像这样的东西:

case 1:
case 2:
case 3:
   return 'blue'
case 4:
case 5:
case 6:
   return 'green'
default:
   return 'red'
const ranges = [
  {
     range: value => value > 0 && value < 0.5,
     value: () => 'red'
  }
  ...
]

switch (myValue) {
  for (let i = 0; i < ranges.length; i++) {
    case [ranges[i].range](myValue): ranges[i].value;
  }
  default:
    return 'some default value';
}
因为范围可能非常大,并且有小数,比如:10.000到50.000之间

但可能是这样的:

case 1:
case 2:
case 3:
   return 'blue'
case 4:
case 5:
case 6:
   return 'green'
default:
   return 'red'
const ranges = [
  {
     range: value => value > 0 && value < 0.5,
     value: () => 'red'
  }
  ...
]

switch (myValue) {
  for (let i = 0; i < ranges.length; i++) {
    case [ranges[i].range](myValue): ranges[i].value;
  }
  default:
    return 'some default value';
}
常数范围=[
{
范围:值=>value>0&&value<0.5,
值:()=>“红色”
}
...
]
开关(myValue){
for(设i=0;i
以上只是一个例子,我正在成像。有人能解决这个问题吗

谢谢你的帮助