Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 短(er)手最小最大百分比?_Javascript_Ecmascript 6 - Fatal编程技术网

Javascript 短(er)手最小最大百分比?

Javascript 短(er)手最小最大百分比?,javascript,ecmascript-6,Javascript,Ecmascript 6,我有一个函数,它将两个值转换为一个范围内的百分比,在本例中是15000到100000之间。感觉很笨重。有没有一种更简单、更容易理解的表达方式 price2percent = (sale) => { let price = sale.soldPrice / sale.livingArea; // Specifically these values price = price > 100000 ? 100000 : price; price = price

我有一个函数,它将两个值转换为一个范围内的百分比,在本例中是15000到100000之间。感觉很笨重。有没有一种更简单、更容易理解的表达方式

price2percent = (sale) => {
    let price = sale.soldPrice / sale.livingArea;
    // Specifically these values
    price = price > 100000 ? 100000 : price;
    price = price < 15000 ? 15000 : price;

    return (price - 1500) / 85000;
} 
price2percent=(销售)=>{
让价格=sale.soldPrice/sale.livingara;
//特别是这些值
价格=价格>100000?100000:价格;
价格=价格<15000?15000:价格;
退货(价格-1500)/85000;
} 

您可以使用
Math.min
Math.max
来约束范围:

const adjustedPrice = Math.min(
  100000,                // can be no higher than 100000
  Math.max(price, 15000) // can be no lower than 15000
);
return (adjustedPrice - 1500) / 85000;
另一个选项是嵌套条件,这将减少不必要的重新分配的数量,尽管它并不能使代码更加清晰:

const调整价格=
价格>100000?100000 :
价格<15000?15000:价格

您可以使用
Math.min
Math.max
来约束范围:

const adjustedPrice = Math.min(
  100000,                // can be no higher than 100000
  Math.max(price, 15000) // can be no lower than 15000
);
return (adjustedPrice - 1500) / 85000;
另一个选项是嵌套条件,这将减少不必要的重新分配的数量,尽管它并不能使代码更加清晰:

const调整价格=
价格>100000?100000 :
价格<15000?15000:价格

我通常使用此实用程序进行以下操作:

const clamp = (value, min, max) => value > min? value < max? value: max: min;

price2percent = (sale) => {
    let price = clamp(sale.soldPrice / sale.livingArea, 15000, 100000);    
    return (price - 1500) / 85000;
}
const-clamp=(值、最小值、最大值)=>value>min?值<最大值?值:最大值:最小值;
价格2%(销售额)=>{
价格=夹具(sale.soldPrice/sale.livingArea,15000,100000);
退货(价格-1500)/85000;
}
我发现它比
Math.min(max,Math.max(min,value))
构造更具可读性


缺点是,在其当前版本中,它不能很好地与
NaN

配合使用。我通常将此实用程序用于以下情况:

const clamp = (value, min, max) => value > min? value < max? value: max: min;

price2percent = (sale) => {
    let price = clamp(sale.soldPrice / sale.livingArea, 15000, 100000);    
    return (price - 1500) / 85000;
}
const-clamp=(值、最小值、最大值)=>value>min?值<最大值?值:最大值:最小值;
价格2%(销售额)=>{
价格=夹具(sale.soldPrice/sale.livingArea,15000,100000);
退货(价格-1500)/85000;
}
我发现它比
Math.min(max,Math.max(min,value))
构造更具可读性

缺点是,在其当前版本中,它不能很好地与
NaN
配合使用

有没有一种更简单、更容易理解的表达方式

price2percent = (sale) => {
    let price = sale.soldPrice / sale.livingArea;
    // Specifically these values
    price = price > 100000 ? 100000 : price;
    price = price < 15000 ? 15000 : price;

    return (price - 1500) / 85000;
} 
使用if/else条件易于理解:

price2percent = (sale) => {
  let price = sale.soldPrice / sale.livingArea;
  if(price > 100000) price = 100000
  else if(price < 15000) price = 15000
  return (price - 1500) / 85000;
} 
price2percent=(销售)=>{
让价格=sale.soldPrice/sale.livingara;
如果(价格>100000)价格=100000
如果(价格<15000)价格=15000,则为其他
退货(价格-1500)/85000;
} 
可以用较短的形式表示为:(较难的方式)

price2percent=(销售)=>{
让价格=sale.soldPrice/sale.livingara;
价格=价格>100000?100000:(价格<15000?15000:价格)
退货(价格-1500)/85000;
} 
有没有一种更简单、更容易理解的表达方式

price2percent = (sale) => {
    let price = sale.soldPrice / sale.livingArea;
    // Specifically these values
    price = price > 100000 ? 100000 : price;
    price = price < 15000 ? 15000 : price;

    return (price - 1500) / 85000;
} 
使用if/else条件易于理解:

price2percent = (sale) => {
  let price = sale.soldPrice / sale.livingArea;
  if(price > 100000) price = 100000
  else if(price < 15000) price = 15000
  return (price - 1500) / 85000;
} 
price2percent=(销售)=>{
让价格=sale.soldPrice/sale.livingara;
如果(价格>100000)价格=100000
如果(价格<15000)价格=15000,则为其他
退货(价格-1500)/85000;
} 
可以用较短的形式表示为:(较难的方式)

price2percent=(销售)=>{
让价格=sale.soldPrice/sale.livingara;
价格=价格>100000?100000:(价格<15000?15000:价格)
退货(价格-1500)/85000;
}